Hi!
I've just been struggling with Hibernates lazy fetch consept vs closed Sessions, but I think I found an solution (at least for now) though.
The solution I'm going for is to use Hibernate.initialize( obj ) on objects/collections before I close the session, but this made me start thinking;
how is actually Hibernate.initialize( obj ) working ?
I mean: The reason we dont use lazy=false on associations is the risk (in worst case) for Hibernate to fetch the whole database - right?
But how is Hibernate.initialize working vs lazy=false on associations?
To clarifying what I am talking about:
I have a method where I am receiving a List with User object. To access/use objects or accociations from that list I need to use Hibernate.initialize on those objects/collections like this:
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List list = session.createCriteria(Project.class).setCacheable(true).list();
// Initializing the Objects needed within the list
for ( int i = 0; i < list.size(); i++ ){
Project p = (Project) list.get( i );
Hibernate.initialize( p.getUsers() ); // another list
Hibernate.initialize( p.getUser() ); // obj
Hibernate.initialize( p.getClient() ); // obj
}
session.getTransaction().commit();
return list;
What is Hibernate.initialize doing, and how deep within the object-graph does it affect?
Here is an article I found on the issue for people with the lazy fetch problem :
http://www.javalobby.org/java/forums/t20533.html
Thanks in advance