-->
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.  [ 4 posts ] 
Author Message
 Post subject: What's difference between Session's load and get methods ?
PostPosted: Mon Jul 03, 2006 6:17 am 
Newbie

Joined: Mon Jul 03, 2006 6:08 am
Posts: 2
Hi gurus,

According to the API doc, the load method throws an exception if the persistent instance doesn't exist.
The get method returns null instead. Is this the only difference ?

Why I'm asking this silly question is that the behavior of the persistent instance retrieved by load(...)
and get(...) methods after closing session is different.
Accessing the instance retrieved by load(...) after closing session would throw an exception (InitializationException).
But not for the instance retrieved by get(...) method. It doesn't throw the exception and its properties are accessible.

I assumed that's because the instance retrieved by load(...) is actually a proxy object so accessing the instance
after closing session is detected by the proxy object and would throw an exception. However, the instance retrieved
by get(...) is NOT a proxy object so that accessing the instance cannot be detected. Therefore, accessing the instance
retrieved by get(...) doesn't throw any exceptions and its properties are of course accessible.

Is my assumption correct ?
( I don't think so though... if it is correct, it means I cannot do lazy-fetch on a persistent instance
retrieved by get(...) method since there's no proxy object in it. )

So my questions are :
1. What's difference between Session#load(...) and Session#get(...) other than throwing an exception or not ?
2. Why does accessing a persistent object retrieved by load(...) throw an exception after closing session while one retrieved by get(...) doesn't ?
(If possible, could someone briefly explain me the underlining implementation of those two methods ?)


Cheers,

-Wolfgang


Top
 Profile  
 
 Post subject: Re: What's difference between Session's load and get methods
PostPosted: Mon Jul 03, 2006 7:16 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
wolfgang2006 wrote:
So my questions are :
1. What's difference between Session#load(...) and Session#get(...) other than throwing an exception or not ?
2. Why does accessing a persistent object retrieved by load(...) throw an exception after closing session while one retrieved by get(...) doesn't ?
(If possible, could someone briefly explain me the underlining implementation of those two methods ?)


You should have a look here : http://www.hibernate.org/hib_docs/v3/re ... te-loading

You'll understand all you're asking for.

Briefly, you get the main facts :
* load() returns a lazyloaded dynamic-proxy.
* get() returns a "real" instance, though directly accessing the data in database.

That said, the answer to your second question is quite obvious : accessing the reference that has been loaded by load() triggers the database query, right ? So you see why you're receiving a LazyInitializationException with load, if you close the session before accessing the data.

With load, you HAVE to access a property of the object to be able to access it after closing the session. If not, you'll get LazyInitException. But if you do it, the proxy will have been loaded and you won't get this exception anymore, even if you access it once more after closing the session.

Example 1 :
Code:
   Session session = HibernateUtil.currentSession();
   session.beginTransaction();
   
   logger.debug("before load()");
   Client c = (Client) session.load(Client.class, "1");
   logger.debug("after load()");
   
   session.getTransaction().commit();
   session.close();
   
   logger.debug(c);

This code throws the LazyInitException.

Example 2 :
Code:
   Session session = HibernateUtil.currentSession();
   session.beginTransaction();
   
   logger.debug("before load()");
   Client c = (Client) session.load(Client.class, "1");
   logger.debug("after load()");

   //access the client before closing the session
   logger.debug(c);

   session.getTransaction().commit();
   session.close();
   
   logger.debug(c);

This code does NOT throw the LazyInitException.

To sum up, depending on what you want to do, you decide if you want to load() or get() your data, for example, you could use load() if you don't want data to be always loaded. Personnally, most of the time, I use get, because of the inherent risk caused by the lazy-loading. So I always load completely my data when I need it.

OK ?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Last edited by batmat on Tue Jul 04, 2006 2:40 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Great !
PostPosted: Mon Jul 03, 2006 10:43 pm 
Newbie

Joined: Mon Jul 03, 2006 6:08 am
Posts: 2
Wow, u r da bomb !

I now fully understand the fact behind the scene.

Thanks a trillion !

-wolfgang
ps)
How can I give u two more credits ? :)


Top
 Profile  
 
 Post subject: Re: Great !
PostPosted: Tue Jul 04, 2006 2:36 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
wolfgang2006 wrote:
How can I give u two more credits ? :)


This way ? :-)

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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