-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate.initialize not work, session open
PostPosted: Mon May 21, 2012 12:57 pm 
Newbie

Joined: Wed Jan 12, 2011 1:15 pm
Posts: 11
Hello, I have written a initialize collections method in the generic DAO for I can invoke since view layer through ManagedBean. I use Spring 3 and JSF2, the DAOs extends HibernateDAOSupport and I use getHibernate of this class for to work with database. Many collections are lazy and my idea is to initialize this collections with unique method and not having to writte a specific method for class/object.


This is the code:

Code:
    public void inicializarColecciones(Object... colecciones){
       logger.trace("GenericoImpl.inicializarColecciones");
      
      SessionFactory session=getHibernateTemplate().getSessionFactory();
      session.openSession();
   
      
      for(Object coleccion: colecciones){
         Hibernate.initialize(coleccion);
      }
   
      session.close();
      
    }


I have checked that the session is open, but throw this exception: ERROR [org.hibernate.LazyInitializationException] (http-localhost-127.0.0.1-8080-3) failed to lazily initialize a collection of role: org.pfc.modelo.Asociacion.miembros, no session or session was closed: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.pfc.modelo.Asociacion.miembros, no session or session was closed

why occurs this behavior? it is correct??

Kind regards.


Top
 Profile  
 
 Post subject: Re: Hibernate.initialize not work, session open
PostPosted: Tue May 22, 2012 2:20 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi Parche,

every persistent collection has a internal reference to a hibernate session
which corresponds to the session in which the owner entity was read.
If that session is already closed when you initialize the collection, then the LazyInitializationException raises.

In you code you seem to assume, that the collections should use something like SessionFactory().getCurrentSession
for initializations but that isn't the case.
You must force the collections to use your session, instead to the memorized one, like following:

Code:
SessionFactory sessionf=getHibernateTemplate().getSessionFactory();
      Session s =sessionf.openSession();
   
     
      for(Object coleccion: colecciones){
         ((PersistentCollection) coleccion).setCurrentSession(s);
         Hibernate.initialize(coleccion);
      }
      session.close();


This works fine for read-only purposes. Changes to your collections will not be persisted to database.


Top
 Profile  
 
 Post subject: Re: Hibernate.initialize not work, session open
PostPosted: Tue May 22, 2012 10:44 am 
Newbie

Joined: Wed Jan 12, 2011 1:15 pm
Posts: 11
Thank for your response, I have tried your code but the method setCurrentSession(Session session) is not validate,it is necesary SessionImplementor) I have modified the code,but my code not work:

Code:
      SessionFactory sessionf=getHibernateTemplate().getSessionFactory();
      SessionImplementor session=(SessionImplementor)sessionf.openSession();
   
      
      for(Object coleccion: colecciones){
         ((PersistentCollection)coleccion).setCurrentSession(session);
         Hibernate.initialize(coleccion);
      }
   
      try {
         session.connection().close();
      } catch (SQLException e) {
         e.printStackTrace();
      }
      
      
    }


I use Hibernate 3.5.6.

Trace error is the same:

ERROR [org.hibernate.LazyInitializationException] (http-localhost-127.0.0.1-8080-3) failed to lazily initialize a collection of role: org.pfc.modelo.Asociacion.miembros, no session or session was closed: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.pfc.modelo.Asociacion.miembros, no session or session was closed

Any suggestions?

Regards.

edit:

I have tried to invoke setCurrentSession with StatelessSession,AbstractSession and Session with this paramert not work because this method need how argument their respective implementations.


Top
 Profile  
 
 Post subject: Re: Hibernate.initialize not work, session open
PostPosted: Thu May 24, 2012 2:44 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Sorry I was wrong,
to me it works, when attaching the collections-owner to the persistent context first, see code below.
N.B.: I use Hiberante 3.6

Code:
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);
}


Top
 Profile  
 
 Post subject: Re: Hibernate.initialize not work, session open
PostPosted: Thu May 24, 2012 6:27 am 
Newbie

Joined: Wed Jan 12, 2011 1:15 pm
Posts: 11
Great solution¡ This code saving time and improving efficiency in the app. Can you tell me in that book or reference exists information about this? Thank you very much for your response.

Kinds regards.


Top
 Profile  
 
 Post subject: Re: Hibernate.initialize not work, session open
PostPosted: Thu May 24, 2012 7:51 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I fear there's no book containing this information (at least book "Java Persistence with Java" does'nt).
I found this solution by looking at the exception stacktrace and looking the code of Hibernate.


Top
 Profile  
 
 Post subject: Re: Hibernate.initialize not work, session open
PostPosted: Thu May 24, 2012 11:33 am 
Newbie

Joined: Wed Jan 12, 2011 1:15 pm
Posts: 11
One more time....Fantastic work'¡¡ Thanks.


Regards.


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