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

Discussion in 'Java' started by ses, Apr 13, 2011.

  1. ses

    ses Guest

    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.
    ses, Apr 13, 2011
    #1
    1. Advertising

  2. 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
    Arved Sandstrom, Apr 14, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Maddox
    Replies:
    0
    Views:
    983
    Patrick Maddox
    Aug 1, 2003
  2. davout
    Replies:
    0
    Views:
    542
    davout
    Apr 18, 2004
  3. markla
    Replies:
    1
    Views:
    533
    Steven Cheng
    Oct 6, 2008
  4. AAaron123
    Replies:
    5
    Views:
    413
    Alexey Smirnov
    Apr 14, 2009
  5. Raymond Schanks
    Replies:
    0
    Views:
    645
    Raymond Schanks
    Aug 3, 2010
Loading...

Share This Page