-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate merge() creating new records for relationships
PostPosted: Mon Dec 04, 2006 6:01 pm 
Newbie

Joined: Wed Nov 29, 2006 8:13 pm
Posts: 8
For some reason, I was hoping that when I do a session.merge(Object), hibernate would find all the existing records that have changed and update them appropriately-- even for all relationships. However for the relationships, it seems to create new records entirely rather than updating existing records. Hence if I had a base class with a one-to-many association with a subBase class, this maps to a Base_Table and SubBase_Table respectively. When I insert a record w/ hydration of the base object, I would get something like this--

Code:
Base_Table
id  |  subbase_id
-------------------
1   |  1


SubBase_Table
id  |  text
-----------
1   |  First insertion


When I change the text in the SubBase object to "Second insertion" (base.getSubBase().setText("Second insertion"); and then do a session.merge(base);, I get this behavior--

Code:
Base_Table
id  |  subbase_id
-------------------
1   |  2


SubBase_Table
id  |  text
-----------
1   |  First insertion
2   |  Second insertion


Is this the default behavior of merge()? I was hoping that it would just update the subbase record instead of inserting another, i.e. I would like this to happen on session.merge(base);--

Code:
Base_Table
id  |  subbase_id
-------------------
1   |  1


SubBase_Table
id  |  text
-----------
1   |  Second insertion


How do I get the update-insteadof-insert behavior (if possible)?

-Larry


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 9:18 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
what does your cascade mapping say ?

you might want to review

http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-transitive

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 10:31 am 
Newbie

Joined: Wed Nov 29, 2006 8:13 pm
Posts: 8
For all of my one-to-manys, I annotated them with CascadeType=ALL. I read the document you mentioned and it seems like I'm missing a delete-orphan value, but the CascadeType enumeration does not give me this option (unless it is inherently built into ALL). Is this what's causing an insertion rather than an update?

-Larry


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 10:57 am 
Newbie

Joined: Wed Nov 29, 2006 8:13 pm
Posts: 8
Nevermind. After doing research, I found out that EJB3 does not support DELETE_ORPHAN so I just use hibernate's @Cascade annotation instead. Thanks for your help.

-Larry


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 12:31 pm 
Newbie

Joined: Wed Nov 29, 2006 8:13 pm
Posts: 8
Although I've been able to fix @OneToMany, I'm running into a similar issue with @OneToOne where any update via merge() does not delete the orphan record. I use the EJB3 CascadeType=ALL for my one-to-one annotation. Since there is no such thing as a delete_orphan for one-to-ones, how do I get rid of the orphan records in the child of a one-to-one association?

-Larry


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.