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]).