Victor Elizondo
Victor Elizondo

Reputation: 295

Error Hibernate propperty "Lazy"

Why do i get this error when i set lazy = true on my mapping file, but when i set it false it works right...

>>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.citi.tablero.contraloria.planes.model.db.hibernate.dto.SigTcContraloriaObjetivos.children, no session or session was closed
>>  at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
>>  at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
>>  at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
>>  at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
>>  at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
>>  at org.citi.tablero.contraloria.planes.model.db.hibernate.dao.TableroContraloriaPlanesOperativosDAO.getIndicadores(TableroContraloriaPlanesOperativosDAO.java:47)
>>  at org.citi.tablero.contraloria.planes.ctrl.IndexCtrl.onCreateTree(IndexCtrl.java:59)
>>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>  at java.lang.reflect.Method.invoke(Unknown Source)
>>  at org.zkoss.zk.ui.select.Selectors$ComposerEventListener.onEvent(Selectors.java:681)
>>  at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2742)
>>  at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2713)
>>  at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2654)
>>  at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136)
>>  at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:534)
>>  at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.run(EventProcessingThreadImpl.java:461)

Upvotes: 2

Views: 491

Answers (4)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

You are accessing the property (for which you have done lazy = true) and that triggers the lazy-loading of the property, but the entity itself is in the detached state (meaning it's not in a hibernate session).

For hibernate to lazy load the property the entity itself should be is a hibernate session (i.e. the entity should be in the persistent sate).

Solution is to make hibernate load it before the session is closed directly (or indirectly as a result of transaction completion). Otherwise, you would have to make it persistent again when another session is opened (that's what the merge() method is for). Or, reload the entity again in the newly opened session (for this you can use the load() method).

Upvotes: 2

Branislav Lazic
Branislav Lazic

Reputation: 14806

Clean solution would be to set this property to true in hibernate.cfg.xml file

<property name="hibernate.enable_lazy_load_no_trans">true</property>

Upvotes: -1

eternay
eternay

Reputation: 3814

You are trying to use a property of your entity that wasn't loaded and you need an open session to do it.

Upvotes: 0

Robe Elckers
Robe Elckers

Reputation: 977

Because you are probably calling the getter from outside your transaction. Lazy loading works only from inside the same transaction as where you load the entity. And the entity you call a lazy getter on has to be managed by the entitymanager.

Upvotes: 0

Related Questions