-->
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.  [ 8 posts ] 
Author Message
 Post subject: Why does Hibernate Entity Manager load object before merge?
PostPosted: Fri Dec 07, 2007 11:17 am 
Beginner
Beginner

Joined: Mon Dec 26, 2005 3:29 pm
Posts: 23
Hibernate version: 3.2.5

We've being using Hibernate for many years now and we love it. Recently, we found that the Hibernate Entity Manager - when asked to merge using a long transaction - will actually load the object again from the database (if its not available in the first or second level cache). Why does it do this? Why does it not simply use the detached object? Hibernate saveOrUpdate doesn't load the object, why does merge?

In addition to inefficiency, this causes problems when you load object using native queries. Hibernate will generate its own query during merge and this may clash with the mapping.

I miss the older, simpler Hibernate days...


Top
 Profile  
 
 Post subject: Re: Why does Hibernate Entity Manager load object before mer
PostPosted: Fri Dec 07, 2007 1:15 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Isaac Goldstein wrote:
Hibernate version: 3.2.5

We've being using Hibernate for many years now and we love it. Recently, we found that the Hibernate Entity Manager - when asked to merge using a long transaction - will actually load the object again from the database (if its not available in the first or second level cache). Why does it do this? Why does it not simply use the detached object? Hibernate saveOrUpdate doesn't load the object, why does merge?

In addition to inefficiency, this causes problems when you load object using native queries. Hibernate will generate its own query during merge and this may clash with the mapping.

I miss the older, simpler Hibernate days...


It has something to do with semantics of merge. It is hard for a container to track all changes in an object while it has been detached. Look at section 10.7. Automatic state detection of hibernate documentation. You might want to use a different method rather than merge.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 1:48 pm 
Beginner
Beginner

Joined: Mon Dec 26, 2005 3:29 pm
Posts: 23
There is no other method for updating. I just don't understand why Hibernate can't use the detached object itself. So a complex object graph will now get loaded before each update? There's got to be a better way...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 5:38 pm 
Beginner
Beginner

Joined: Tue Apr 24, 2007 12:53 pm
Posts: 28
There are actually several other methods for updating an entity: persist, save, saveOrUpdate, and replicate.

The real question is what are you trying to do?

The merge method is designed to update the database, without re-associating the instance with the Session (it is expected that it is already associated with the session).

The persist method will write out the instance.

The save will persist the object to the database and generate a new ID in the process. (Should be a new instance <read "row" > for the table.

The update will take an existing object and update that row in the database based on the ID of the object

saveOrUpdate: will do save or update depending on the state of the object.

I believe, that save, update, and saveOrUpdate all re-associate the object with the session (someone correct me if i'm wrong). I've run into a Hibernate error where there already exists an instance with a particular ID associated with the Session. I'm not sure if my solution for this problem is correct but: check for this error and run a merge() to persist my info.

Hope this helps.

-B


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 10:59 am 
Beginner
Beginner

Joined: Mon Dec 26, 2005 3:29 pm
Posts: 23
I am using Hibernate Entity Manager, not Session. The only method to update from Entity Manager is merge. Of course, I can write a Entity Manager proxy and redirect to Session.saveOrUpdate, but I was wondering about the design approach. Why did Hibernate decide to load before update? At least in core Hibernate you can control this with the select-before-update setting for class.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 11:15 am 
Beginner
Beginner

Joined: Tue Apr 24, 2007 12:53 pm
Posts: 28
Sorry, I misunderstood the question/situation. I think that this question would best be posed in the other forum (Hibernate Annotations, EntityManager, Search, Validator, Shards). I only say that because I noticed that one of the Hibernate Team seems to monitor that one closely (emmanuel), at least they have since i posed my question and have been watching for a response over the last few days.

-B


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 11:21 am 
Beginner
Beginner

Joined: Mon Dec 26, 2005 3:29 pm
Posts: 23
Thanks. Silly of me...


Top
 Profile  
 
 Post subject: Re: Why does Hibernate Entity Manager load object before merge?
PostPosted: Mon Aug 02, 2010 4:07 pm 
Newbie

Joined: Fri Oct 06, 2006 8:09 pm
Posts: 5
Location: Costa Mesa, CA
Here is emmanuel's response: viewtopic.php?p=2371627


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