-->
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.  [ 9 posts ] 
Author Message
 Post subject: when would you use Session.load versus Session.get?
PostPosted: Wed Jan 04, 2006 8:47 pm 
Beginner
Beginner

Joined: Sun Jun 05, 2005 9:45 am
Posts: 27
I just read the API documentation (I quoted it below) and I am curious when would you use Session.load over Session.get? When would you want a situation where hibernate returns a proxied object that represents nothing?

Quote:
Session.get:
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. (If the instance, or a proxy for the instance, is already associated with the session, return that instance or proxy.)


Quote:
Session.load:
Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.

You should not use this method to determine if an instance exists (use get() instead). Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.


Regards,

Joshua


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 9:13 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
A proxy wraps an identifier. That is often all you need, an object that looks like the real thing, but didn't cost a database hit.

session.load(buyer);
item.setBuyer(b);


Top
 Profile  
 
 Post subject: Thanks for your response
PostPosted: Thu Jan 05, 2006 9:35 am 
Beginner
Beginner

Joined: Sun Jun 05, 2005 9:45 am
Posts: 27
Christian,

I use Session.get to retrieve an object that does not exist, null is returned. If I use Session.load to retrieve the same object, I get back an object which throws an ObjectNotFoundException when I try to access any of its attributes.

When would you want an object back when you were not supposed to get one?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 9:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
to associate objects without unnecessary db access.

order.setCustomer(session.load(Customer.class, custid));

The customer object is never loaded from the database.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 11:33 am 
Newbie

Joined: Sun Oct 09, 2005 6:36 am
Posts: 19
You need to be a bit carefull when you are using inheritance though. Assume you have class A and the classes B and C which both extend A. When you use load(A, PKEY) you get a proxy that just implements A even if the object behind PKEY is a B or C as hibernate doesn't access the DB so it cannot know that the object is actually a B or C. When you call one of the mehtods that are defined in A the object itself knows that it is a B and works as expected. But you cannot cast the proxy you got from load() to B.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 11:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes - there is no free lunch, you need to know what you are doing ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 4:12 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Quote:
to associate objects without unnecessary db access.

order.setCustomer(session.load(Customer.class, custid));

The customer object is never loaded from the database.


great naming - session.load(...) load never ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 6:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Only when there is a class level proxy...


Top
 Profile  
 
 Post subject: load() versus get()
PostPosted: Fri Mar 10, 2006 1:16 pm 
Beginner
Beginner

Joined: Wed Nov 30, 2005 4:58 pm
Posts: 22
Location: Austin, TX
Suppose you get a proxy via load(). Is there a cheap call you can make to check row existence before you proceed with calling methods on the proxy? Checking for null after a get() is pretty cheap, but does get() cost you that much more than load()? Will get() also return a proxy?


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