-->
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.  [ 9 posts ] 
Author Message
 Post subject: LazyInitializationException
PostPosted: Fri Mar 24, 2006 3:44 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 3:42 pm
Posts: 32
Hy everyone,


When i use hibernate in my applications i always set the lazy to false because usually i don`t need set lazy = true. But i`m in a little big application now and there are many lists and objects in one class for i load everything. I`m trying to use a lazy load but when i`m my trying retrieve my collection i`m getting this exception:

Code:
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection (daten.xmanager.model.Computador.perifericos) - no session or session was closed
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:180)
   at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:61)
   at org.hibernate.collection.PersistentSet.add(PersistentSet.java:158)
   at daten.xmanager.model.Computador.setPerifericos(Computador.java:83)
   at daten.xmanager.negocio.SistemaFacade.main(SistemaFacade.java:133)



I have tested the session and it is open. Any idea is good for me.

Thanks,

Albertp


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 4:22 pm 
Beginner
Beginner

Joined: Sun Feb 19, 2006 3:50 am
Posts: 34
Is it the same session you used to retrieve the object in the first place? If not, I think you'll need to reattach the object to the session using session.lock(someObject, LockMode.NONE).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 4:58 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 3:42 pm
Posts: 32
Really? I think i have a big problem now. I`m just passing the id and set the identifier in the namequerie. Are you sure about that? I think it`s a little bit crazy. Isn`t there other way?

Alberto


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 5:45 pm 
Beginner
Beginner

Joined: Sun Feb 19, 2006 3:50 am
Posts: 34
In order for lazy loading to work, the object cannot be detached from the session when you try to access the lazy-loaded property. This means that if you load someObject and then session.evict(someObject) or session.close(), accessing the lazy property will fail unless you reattach someObject to a new session using newSession.lock(someObject, LockMode.NONE).

There's no way around this. If the object is detached from the session, you cannot access lazy-loaded properties.

The solution is to call session.lock(someObject, LockMode.NONE) before calling someObject.getLazyProperty(). That should reattach the object to the new session and allow you to lazy load the property.

If you still don't understand, please post the code you're having trouble with.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 9:01 pm 
Newbie

Joined: Mon Jan 23, 2006 2:57 pm
Posts: 1
If you have cases where you know the collection will be needed after the session in which it was fetched has been closed you can manually initialize it before you close the session. Check the javadocs for org.hibernate.Hibernate.initialize(Object proxy)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 25, 2006 4:48 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 3:42 pm
Posts: 32
Ok, so if i want execute the query someObject left fecth join other object i have to use session.lock(someObject, LockMode.NONE)? It's not good, i don't understand why do i have to do this? the only thing that hibernate has to do is execute my query and populate my object. My code is:

Code:
  object = dao.getById(id);//open and close the session.
  //a long time after
  object.setListObject(dao.getNameQuerie(load.object))


When i try to execute the last i receive the error message. Thanks for the help guy.

Alberto


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 25, 2006 7:23 pm 
Beginner
Beginner

Joined: Sun Feb 19, 2006 3:50 am
Posts: 34
It's not your query that's throwing the error, it's the perifericos collection. When you initially loaded the Computador object, the perifericos collection was not initialized because it is set to lazy-load. The perificos collection cannot be initialized if the Computador object is not attached to a session. Since you closed the session that loaded the Computador object before the perifericos collection was initialized, you MUST reattach the Computador object to a session before using the perifericos collection or it will throw an error. Period. End of story. In order to reattach it to a session you must do session.lock(computadorObject, LockMode.NONE).

Your other option is to do what bugg says and initialize the perifericos collection before you close the session that loaded the computador object, but then what's the point of having the collection be lazy loaded?

Those are your only options as far as I know.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 8:25 am 
Beginner
Beginner

Joined: Fri Sep 02, 2005 3:42 pm
Posts: 32
Hy guys, thanks for the help. I`m having other problem now, i followed the steps that you said and reattached the object to the session. But i`m getting this error now:

Code:
  Exception in thread "main" org.hibernate.HibernateException: reassociated object has dirty collection reference (or an array)
   at org.hibernate.event.def.OnLockVisitor.processCollection(OnLockVisitor.java:67)
   at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:104)
   at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:64)
   at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:58)
   at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:129)
   at org.hibernate.event.def.AbstractReassociateEventListener.reassociate(AbstractReassociateEventListener.java:75)
   at org.hibernate.event.def.DefaultLockEventListener.onLock(DefaultLockEventListener.java:57)
   at org.hibernate.impl.SessionImpl.lock(SessionImpl.java:472)
   at daten.xmanager.model.dao.impl.HibernateComputadorDAO.carregarDependencia(HibernateComputadorDAO.java:55)
   at daten.xmanager.model.dao.impl.HibernateComputadorDAO.carregarDependencia(HibernateComputadorDAO.java:47)
   at daten.xmanager.negocio.models.ComputadorBO.carregarDependencia(ComputadorBO.java:56)
   at daten.xmanager.negocio.SistemaFacade.listarPerifericosComputador(SistemaFacade.java:97)
   at daten.xmanager.negocio.SistemaFacade.main(SistemaFacade.java:193)



If you know why, please help me agaiin :).

Alberto


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 2:05 pm 
Beginner
Beginner

Joined: Sun Feb 19, 2006 3:50 am
Posts: 34
Hmm, unfortunately I've never run into that one before. I found this on the forums with a quick search though: http://forum.hibernate.org/viewtopic.php?t=934551.

Read that thread and see if any of it helps. The first thing I would try is using session.update(computadorObject) instead of session.lock and see if it helps. Also make sure you're not doing anything special in the setter method for your perifericos collection. See the thread I link to for more information about the setter if session.update() doesn't fix the problem.


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