-->
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: load() and get() - session
PostPosted: Fri Feb 23, 2007 6:03 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 11:39 am
Posts: 24
I read that session is a persistence context, a cache of persistent entity instances. This means that it remembers all persistent entity instances that were handled in a particular unit of work. So when we use the session.load() , Hibernate first checks the persistence context for the current unit of work.And if the entity is found there, no database hits occur.

So from what I understand if I have an object User in a method getUser()
if i do session.load(userId) if this specific User is not saved in the same unit of work, it will not be found correct?

So load works if only I have sth like?

User user = new User();
session.user(save);
session.load(IUser.class, userId);


in the same session I should save a pojo and then get it with load?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 12:50 pm 
Beginner
Beginner

Joined: Sat Aug 19, 2006 8:04 pm
Posts: 30
yes that's correct. load can save you time and make your error handling more robust. you have to be careful though to make sure you use it when you know it should be in the session.

"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."

http://gmarwaha.blogspot.com/2007/01/hi ... sions.html

i need some credits hook it up please, thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 26, 2007 5:39 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 11:39 am
Posts: 24
Thank you for your answer.

But what I don't understand is how would I know if an object is present in session.
For example if in a DAO class I have the getUserInDao() method
that is called by a session bean by a method getUserInBean()
how would I know that if I use session.load(User.class, userId) in the DAO then the object it will be present in session?

For example lets assume that in the getUserInBean() the object was present in session then if I had session.load(User.class, userId) in getUserInDao() I would be sure that it is ok only if the preceding method call before getUserInDao() was the getUserInBean(), but this is not always sure.

Hope I am making sense.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 26, 2007 7:40 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi juanita,

You need not to worry too much .If you have Id value with you then go for session.load. and also you will have to use try catch. If you don’t want to use try catch you could apply null check logic with session.get().prefer is session.load.

If some ID value are coming from external entity JMS web service and you are not sure use session.get.If while in your service layer if any object required on some particular condition use session.load.Because might be the possibility that it is used earlier and it would get from session.These are no hard and fast rule just a hurestic to use it in some good way

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 26, 2007 7:46 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 11:39 am
Posts: 24
dharmendra.pandey wrote:
If you have Id value with you then go for session.load.


Even if have Id value who guarantees that my object is in session?
I might have the id through previous methods but that doesn't mean that
I have loaded before.I might have it only as a parameter.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 26, 2007 4:10 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
If you open up a new session, call session.load(User.class, userId), if that Id exists in the database then that object will be read from the database and persisted in that session for the lifetime of that session.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 3:54 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 11:39 am
Posts: 24
kochcp wrote:
If you open up a new session, call session.load(User.class, userId), if that Id exists in the database then that object will be read from the database and persisted in that session for the lifetime of that session.


Just a minute, because I got confused now:). I thought that by using session.load(User.class, userId) Hibernate does not hit the database but gets the object from the session if exists there. My problem is if I have for example a UserBean and a UserDAO class:

UserBean

public User getUser() {

User user = session.getUser(User.class, userId);
userDao.checkUser(user.getId());
}


UserDAO


public User checkUser() {
session = sessionFactory.getCurrentSession();
session.load(User.class, userId);


What happens here? If checkUser is called after getUser I understand that the object is present in session
but if it is called after another method where session.getUser(User.class, userId)
is not called there won't I have an exception?

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 10:00 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
juanita wrote:
What happens here? If checkUser is called after getUser I understand that the object is present in session
but if it is called after another method where session.getUser(User.class, userId)
is not called there won't I have an exception?

}


no, there should be no exception. if it's not in the session, it'll just hit the database and load it.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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.