-->
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.  [ 2 posts ] 
Author Message
 Post subject: Difference between session.get() & session.load()
PostPosted: Thu Oct 13, 2005 10:02 am 
Newbie

Joined: Thu Oct 13, 2005 9:39 am
Posts: 1
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5


Hello,

I am fairly new to hibernate and I'm trying to understand the difference between session.get() and session.load(). I ask this because I was using session.get() to retrieve a persistant object in my web application. I updated a field in the persistant object and called a session.save(). This caused a hangup in hibernate and my database. This only occurs if I retrieve the object via session.get() and not session.load(). This also brings me to the next question, what is the difference between using session.save() and session.update() for performing updates. Is there a best practices for using sesion.load() vs session.get() and session.save() vs session.update().

Thanks.

Adam


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 10:21 am 
Newbie

Joined: Wed Oct 12, 2005 7:06 pm
Posts: 10
session.get is used to create a new persistent object by id from the database. It returns null if there was no such object in the database. session.load is similar except that if there was no such object in the database it throws an exception.

session.save on a transient item will assign it a id and make the object persistent: i.e., ensure it, and any other objects it refers to, get saved to the database. This operation essentially calls an SQL INSERT to be executed. Any further calls to the mutators of the object within the transaction will cause an SQL UPDATE to be invoked.

session.lock and session.update are both intended for reattaching a detached object. Normally you should use session.update which triggers an SQL UPDATE to the database row with id equal to that of the object. Thus if the database and the object disagreed on the values contained, then the object overrides the database. session.lock simply reattaches the object (to the session) without checking or updating the database on the assumption that the database is still fully in synch with the object. Generally, do not use this method unless you are absolutely sure that nothing has changed the database state of the object since it was detached or if it does not matter because you will be overwriting all columns that could have changed anyway later on in the transaction.

session.saveOrUpdate is a convenience method that checks whether the object is transient, in which case it acts like session.save, or detached, in which case it acts like session.update.

Lachu


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