-->
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.  [ 11 posts ] 
Author Message
 Post subject: get vs load
PostPosted: Tue Feb 17, 2004 5:35 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:33 am
Posts: 29
Sorry if this has been already answered, but I couldn't find
anything searching on the forums (looking for "get" and "load" is too
generic...).
I don't understand the difference between loading an object using
Session.load and using Session.get.

What does it mean "assuming that the instance exists" in the "load" docs?
What happens when the instance doesn't exist?
Is there a performance difference between Session.get and Session.load?

Thank you in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 5:41 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
The main difference between load and get is that get will always hit the database and return the requested object ( or null if it's not there ) whereas load ( if you are using a lazy/proxied class ) will not hit the database on the load call. All it does it make a proxy class, fills in the primary key and leaves it. The database is only accessed when you request a property of that object which will lead to unexpected errors if the object doesn't exist. Therefore only use load for objects that definately do exist, if you're not sure or want to check, use get.

Cheers.

Myk.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 5:49 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:33 am
Posts: 29
Thank you. Only a thing:
what happens when the instance doesn't exist (using load and without lazy/proxy)?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 7:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You get an Exception of course.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 12:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
....but first when you access a non-id attribute on the object....!

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 12:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
without lazy/proxy you get an exception immediately ;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 12:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
of course....busy day ...only reading postings 77% ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 8:21 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Myk wrote:
The main difference between load and get is that get will always hit the database and return the requested object


Will get() hit the DB if the object has already been loaded in that Session?

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 8:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No.


Top
 Profile  
 
 Post subject: get vs load
PostPosted: Thu Mar 25, 2004 11:26 am 
Newbie

Joined: Fri Mar 19, 2004 2:04 pm
Posts: 1
Q:
Quote:
Will get() hit the DB if the object has already been loaded in that Session?

A:
Quote:
No.


This seems dangerous (and annoying) to me. Basically, the behavior of the get() method varies per object based on whether or not it has already been resolved via load(). This matters most when the object has been deleted. But I don't see how the behavior of the get() method can be improved. Tell me whether or not using the following code in my Hibernate facade seems like a good practice:

Code:
public Object get(Class clazz, Serializable id)
{
     Object entity = session.get(clazz, id);
     if (entity != null && isProxy(entity))
     {
          evict(entity);
          entity = get(clazz, id);
     }
     return entity;
}


This way, get always returns an object or null, never a proxy, especially for an object that has been deleted.

I appreciate the feedback.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 8:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It is not at all dangerous or annoying.


If you want to hit the DB, use Session.get(clazz, id, LockMode.READ)


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