-->
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: session.get vs session.load
PostPosted: Mon Jun 13, 2005 9:21 pm 
Newbie

Joined: Tue Sep 21, 2004 1:33 pm
Posts: 5
Hibernate version: - 3.0.5

I think I unstand the difference between session.get and session.load, but can someone explain why you would ever use session.load instead of session.get.

More direct, why would you ever want to get an uninitalized proxy using session.load? The docs are a little unclear on this issue.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 11:38 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
The .load method is useful in situations where you "know" the record exists, and you don't want to have to test the return value each and every time you read in an object from the database. You are asserting that the object does indeed exist in the database when you call the method.

Think of an application that assigns a user ID to a user account whenever it is created. The user account would probably have an associated username candidate key, but the real primary key for the record would be the sequence-assigned user ID. That would likely be the value passed around the application from screen to screen (in a web app, for example).

Now, since the user ID was assigned by your system, then you can assume that all user IDs you load from the database are indeed associated with a user account that exists in the database. In this case, use .load. You can write code such as:

Code:
User user = session.load(User.class, userID);
user.setName(newName);


You don't have to worry about getting a NullPointerException, since the load method's contract is to throw an exception on failure to locate the record. Likewise, you don't have to do any sort of if (user == null) throw ... test, since Hibernate does it for you.

Now, on the other hand, if you had an app that you let the end user enter user IDs in a form field, then you would want to have Hibernate return a null value rather than an exception. This is because it is a normal--rather than abnormal--scenario for your app to receive a user ID for a user that does not exist.

I hope that (my philosophy on) this helps.


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.