-->
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.  [ 3 posts ] 
Author Message
 Post subject: Clone vs Detach (and impact on Session.merge)
PostPosted: Wed Aug 10, 2005 12:29 am 
Newbie

Joined: Wed Aug 10, 2005 12:12 am
Posts: 8
Location: Washington DC
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

My first post....couldn't get closure on this by searching the forums or flipping through my trusty Hibernate In Action.

Here's our scenario that we can not seem to figure out. When we retrieve an object (call it P1), we use an interceptor to clone it (standard implementation of writing/reading to stream) creating P2. That way, any changes we make to the object (P2) are not candidates for flushing. Based on business logic, we may decide to not persist any changes to that object (P2). However, we still want to commit the transaction to persist other items.

Given this approach, P1 is still in the Hibernate Session. When we persist P2, we use the session.merge(P2) method. Without doing that, we would get a NonUniqueObjectException. But with calling merge, we are always seeing unexpected updates to the database (at flush time) even when we change nothing on P2. So P1 state and P2 state are the same. It looked like the merge operation was setting the Collections to dirty? Could that be because we blindly serialized everything?

Would circumstances been different if we detached instead of cloning? Suppose, we detached, and the reattached (using session.lock), would the merge call even be needed?

Thanks in advance Hibernate community.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 12:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This should not happen. merge() should be safe from any behavior like that.

Perhaps you have broken equals()/hashCode()s for collection elements.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 2:16 pm 
Newbie

Joined: Wed Aug 10, 2005 12:12 am
Posts: 8
Location: Washington DC
I double checked those implementations and they are fine.

I was able to narrow down the issue to this.

Let's say you have a Parent and a <set> of Child objects. If in the Parent.hbm.xml file, you put lazy="false" on the <set lazy="false"...> element, then it results in an unnecessary update by Session.merge. If I make the <set lazy="true"...> be lazy="true", then there is no extra update by Session.merge.

This occurs in my test case both when my Parent.getChildren() set is null and also when it contains actual Child objects.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.