Hibernate lazy init collection

By | 2012-10-03

Using Hibernate.Initialize may experience an issue, the session is not yet initialize.

Ref: http://blog.9mmedia.com/?p=272

Add these before that

SessionFactory sf = ((HibernateEntityManagerFactory)entityManagerFactory).getSessionFactory();
Session session = SessionFactoryUtils.getSession(sf, false);
SessionImpl source = (SessionImpl) session;

 

I was using EntityManagerFactory from JPATemplate instead

for PersistantCollection
check here
SessionImplementor sessioni = (SessionImplementor) sessionf.openSession();
Session s = (Session) sessioni;
for(Object coleccion: colecciones){
PersistentCollection coll = (PersistentCollection) coleccion;

// first attach the collections owner to the new persistent context
s.buildLockRequest(LockOptions.NONE).lock(coll.getOwner()); // in Hibernate 3.5 it may be replaced by: s.lock(coll.getOwner(), LockMode.NONE);

Hibernate.initialize(coll);
}

Leave a Reply

Your email address will not be published. Required fields are marked *