EJB entity with multiple parents (only one relationship persists?)

S

ses

I have an EJB entity relationship like the following

Customer ---one-many---> Booking
Location ---one-many---> Booking

Firstly I persist the booking. The problem is when I try to add the
same Booking entity to both Customer and Location, and then merge
customer and location the only relationship which remains in the
database is the one for which merge was called last.

This indicates that an individual Booking can only be 'owned' by one
entity, why is this the case and how can I circumvent it? Any
suggestions would be gratefully appreciated.

I'm using JPA with eager fetch type and orphan removal.
 
A

Arved Sandstrom

I have an EJB entity relationship like the following

Customer ---one-many---> Booking
Location ---one-many---> Booking

Firstly I persist the booking. The problem is when I try to add the
same Booking entity to both Customer and Location, and then merge
customer and location the only relationship which remains in the
database is the one for which merge was called last.

This indicates that an individual Booking can only be 'owned' by one
entity, why is this the case and how can I circumvent it? Any
suggestions would be gratefully appreciated.

I'm using JPA with eager fetch type and orphan removal.

This is not the case: those relationships are possible. There's nothing
unusual about one entity having multiple @ManyToOne fields (in this case
Booking would have two).

Notionally, if you have a pre-existing Location, and a pre-existing
Customer, then doing what you describe inside a transaction (say, within
EntityTransaction boundaries, or within a session bean method that is a
JTA transaction) should work. I can only suggest supplying a pared-down
snippet of your code - extraneous setters are unnecessary, but include
all the EM operations, and everything to do with setting the
bidirectional relationships. I would tend to merge the Customer and
Location first thing, before setting the relationships to the new
Booking, and persisting the Booking, and perhaps you can try that.

Make sure you're using merge correctly, that if you use the Customer and
Location that it's the returned values that are used subsequently.

AHS

--
That's not the recollection that I recall...All this information is
certainly in the hands of the auditor and we certainly await his report
to indicate what he deems has occurred.
-- Halifax, Nova Scotia mayor Peter Kelly, who is currently deeply in
the shit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top