-->
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: Too many updates after session.close()
PostPosted: Wed Jan 14, 2009 9:25 am 
Newbie

Joined: Mon Apr 10, 2006 11:45 pm
Posts: 17
Hibernate version: 3.2.6

Could someone please clarify what the expected behavior of generated update statements are when you close a session? Currently if I do the following:
    open a session
    perform a query
    close the session
    open a session
    call saveorupdate
    close the session
then it generates a SQL updating most of my tables. However, if I don't close the session the first time:
    open a session
    perform a query
    call saveorupdate
    close the session

then it will just generate the sql for updating the one table I changed.

Is this the correct behavior? I can't find anything definitive in the documentation as to whether or not this is happening. I found in the documentation that this isn't how we should be doing it but it would be nice to get some clarification.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 11:14 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
It seems, that this happens because your object-graph is detached after you close first session.
When you call saveOrUpdate in your second session, hibernate does not know, which objects have changed, it just does what you want it to do: saveOrUpdate.

You could call merge instead or enable select-before-update, but in either case your graph will be reloaded. Another solution would be disabling cascade-behaviour and update only the objects, which were modified by yourself.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 12:17 pm 
Newbie

Joined: Mon Apr 10, 2006 11:45 pm
Posts: 17
Does closing the session automatically detach the session? I'm not calling session.detach() anywhere.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 4:10 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Object can only be persistent inside a session, when you close it the objects become detached.

Hibernate does not know if you manipulate 1 or 100 objects outside the session scope so it has to assume the worst.

Working as intended as far as i know


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2009 4:49 pm 
Regular
Regular

Joined: Wed Jan 21, 2009 8:41 pm
Posts: 54
Why does hibernate need to form one query per object when using select-before-update?

Why can't it optimize that query into one select?

pkleindl wrote:
Object can only be persistent inside a session, when you close it the objects become detached.

Hibernate does not know if you manipulate 1 or 100 objects outside the session scope so it has to assume the worst.

Working as intended as far as i know


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.