-->
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.  [ 8 posts ] 
Author Message
 Post subject: what 's different between session.get and session.load?
PostPosted: Fri May 20, 2005 8:03 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
HI everyone:

I want to know if "session.load" don't hit database? Is there any benefit to use session.load other than session.get ? thks!

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 8:08 am 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
session.load gives an exception if the object is not found, session.get returns null


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 11:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Assuming you have a lazy (or proxied) domain object, session load will return the proxy (if its not cached) that may possibly represent a record that does not exist in the database. At times this is a great feature but it may be an issue in others. session.get always hits the database (if its not cached) thus you have a good idea if the record exists or not but at the cost of a database access. You need to decided which is appropriate for your use case.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 21, 2005 3:57 am 
Newbie

Joined: Thu Oct 21, 2004 7:02 am
Posts: 14
David,

I converted from Hibernate v2.x to v3 and ran into problems with the session.load returning proxies when lazy is enabled. My workaround was to use Hibnernate.initialize( ...), so Exceptions are thrown when you would expect them (when using session.load). Is this a good / the right solution?

--
Thanks,
Kristian


Top
 Profile  
 
 Post subject: :(
PostPosted: Sat Jun 04, 2005 8:20 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
david wrote:
session load will return the proxy (if its not cached) that may possibly represent a record that does not exist in the database. .


I think it is very dangrous! You mean that I may display a incorrect recordset to client? Is there any meaning to do this? It is certainly that the client want to see the record that is in the database,right? ;-(

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 04, 2005 11:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
kristiann wrote:
David,

I converted from Hibernate v2.x to v3 and ran into problems with the session.load returning proxies when lazy is enabled. My workaround was to use Hibnernate.initialize( ...), so Exceptions are thrown when you would expect them (when using session.load). Is this a good / the right solution?

--
Thanks,
Kristian


Some people have not noticed (or read about) the change to lazy=true being the default in H3. You could call initialise to verify the objects existance though I feel its not the best approach as I do not like using exceptions to indicate anything other than a fault.
If you want to load the object and make sure that it is present then use session.get() which will return a null if the object did not exist at the time of the read transaction was performed.


Top
 Profile  
 
 Post subject: Re: :(
PostPosted: Sat Jun 04, 2005 11:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Yashnoo wrote:
david wrote:
session load will return the proxy (if its not cached) that may possibly represent a record that does not exist in the database. .


I think it is very dangrous! You mean that I may display a incorrect recordset to client? Is there any meaning to do this? It is certainly that the client want to see the record that is in the database,right? ;-(


Not quite sure what your concern is here. I agree you can display an stale record to the user. This is always a possibility in a multi-user system. Anyway, the proxy allows you to get a key reference without hiting the database. Yes the object with that key may not exist in the database so care is required. This is no different to you getting an object and that object gets deleted by another user just after your load. You have a object which does not exist in the database. An exception at some point will be thrown and you manage it appropriately. The developer is the best person to judge the ballance against db hit cost and life cycle of existing objects.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 1:38 am 
Newbie

Joined: Thu Oct 21, 2004 7:02 am
Posts: 14
Kristian wrote:
Quote:
David,

I converted from Hibernate v2.x to v3 and ran into problems with the session.load returning proxies when lazy is enabled. My workaround was to use Hibnernate.initialize( ...), so Exceptions are thrown when you would expect them (when using session.load). Is this a good / the right solution?

--
Thanks,
Kristian


David wrote:
Quote:
Some people have not noticed (or read about) the change to lazy=true being the default in H3. You could call initialise to verify the objects existance though I feel its not the best approach as I do not like using exceptions to indicate anything other than a fault.


I agree that it doesn't feel like the best approach. But as you say, exceptions should indicate a fault and there are many use cases where not getting the object indicates a fault. My impression is that the load method is for exactly this. Since it returns a proxy, we need some way of throwing an exception if the object doesn't exist. I'm probably misunderstanding something here, but for me it doesn't make any sense that the load method changes behavior depending on if you use lazy loading or not.

David wrote:
Quote:
If you want to load the object and make sure that it is present then use session.get() which will return a null if the object did not exist at the time of the read transaction was performed.


Yes, I use the get() method when this behavior is what I'm looking for. But as I mentioned above, there are cases when non-existence of the object indicates a fault and should result in an exception.

You could check for null and throw an exception accordingly. But this becames very tedious and repetitive.

Best regards,
Kristian


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