-->
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.  [ 3 posts ] 
Author Message
 Post subject: collections and lazy
PostPosted: Thu Oct 09, 2003 7:15 am 
Newbie

Joined: Thu Oct 09, 2003 6:57 am
Posts: 17
Location: France
Hi,

An User has more than one Address.
When I retrieve a user by Id, I don't force to initialize the collection "Addresses".

If after I want to access to the Addresses, I must open the session, initialize Addresses and close session.
Do you think is it a good idea to put this code directly in the getAddresses of User (business) class. Like this :


class User implements Serializable {

Set addresses;

....

public java.util.Set getAddresses() {
if (!this.addresses.isInitialized) {
Session session = SessionBean.getSessionBean().getHibernateSession() //get the hibernate session
session.beginTransaction();
Hibernate.initialize(addresses);
session.close()
}
return this.addresses;
}
}

What do you think about this ?
I searched in Archive but I did not find a good solution for retrieve collections just when we want to use them with one session by HttpRequest.

Thank you,

J


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 8:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
Do you think is it a good idea to put this code directly in the getAddresses of User

Absolutely not. You have to remember that Hibernate itself is going to be calling these methods.

Is your web tier opening and closing Hibernate sessions? Assuming you loaded the user when they logged in and stored it into session and then need to display some stuff later, is that correct? If both are true, when you need to display the user's addresses grab the session for the given request and do:
Code:
if (!Hibernate.isInitialized(user.getAddresses())
{
    session.lock(user, LockMode.NONE);
    Hibernate.initialize(user.getAddresses());
}


Also, you may want to consider not storing the user object into session, or at least timing it out, so you don't run into data consistency issues. Maybe just store their id or username (i.e., the Memento pattern [GoF]).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 9:19 am 
Newbie

Joined: Thu Oct 09, 2003 6:57 am
Posts: 17
Location: France
Absolutely not. You have to remember that Hibernate itself is going to be calling these methods.

Ok, it is what I thank, but I am not sure. It is more clear for me now.

Is your web tier opening and closing Hibernate sessions? Assuming you loaded the user when they logged in and stored it into session and then need to display some stuff later, is that correct?

Yes, thank you for the answer

[...]


Also, you may want to consider not storing the user object into session, or at least timing it out, so you don't run into data consistency issues. Maybe just store their id or username (i.e., the Memento pattern [GoF]).

Yes, good, I will see that.


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