Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Object Relational Mapping and the fly likes Hibernate and extra insert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate and extra insert" Watch "Hibernate and extra insert" New topic

Hibernate and extra insert

Marco Tulio Borges
Ranch Hand

Joined: Sep 14, 2004
Posts: 45
I have a problem with Hibernate (Annotations). Here are my Entities:

This one works just fine. The other one that does not is here:

Now, the problem: before I persist ProblemEntity, I call:

After this code, I try to save my ProblemEntity and then flush the session object (the same session that retrieved e1). Even though e1 is not transient and has a valid ID (I checked it), Hibernate tries to insert both problemEntity and e1 (in this order). What am I doing wrong? (both classes are mapped in hibernate.cfg.xml)
[ May 21, 2008: Message edited by: Marco Tulio Borges ]

.: SCJP 1.4 (95%) / SCWCD 1.4 (91%) / SCBCD (77%) :.
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us." - Calvin
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265


It looks like you need to pick which side of the association is the owner, and on the annotation for the other side, include the mappedBy attribute on the inverse side. This will let the owning side of the association manage the relationship, and tell the inverse side it is not responsible for maintaining that relationship.

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
I agree. Here's the link:
subject: Hibernate and extra insert
jQuery in Action, 3rd edition