Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #3056

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

From Arved Sandstrom <asandstrom3minus1@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: EJB entity with multiple parents (only one relationship persists?)
References <0fb6a143-c58b-45b5-b6a8-26dac6883107@17g2000prr.googlegroups.com>
Message-ID <qGrpp.9786$8U5.9241@newsfe20.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2011-04-13 22:00 -0300

Show all headers | View raw


On 11-04-13 06:51 PM, ses wrote:
> 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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar


Thread

EJB entity with multiple parents (only one relationship persists?) ses <stewart@ssims.co.uk> - 2011-04-13 14:51 -0700
  Re: EJB entity with multiple parents (only one relationship persists?) Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-04-13 22:00 -0300

csiph-web