org.hibernate.LazyInitializationException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • I posted the problem on the Hibernate-forum (https://forum.hibernate.org/viewtopic.php?f=1&t=1003277) and got no answer. So I assume this is a bug. I wrote a custom PropertyAccessor {code} public class MyPropertyAccessor implements PropertyAccessor{code} with this code within the set-Method of the Setter: {code} public void set(Object target, Object value, SessionFactoryImplementor factory) throws HibernateException { try { if (((List) value) == null) { return; } List tempList = new ArrayList(); tempList.addAll((List) value); // ... more code here{code} What I get during runtime is a "LazyInitializationException: illegal access to loading collection" (Stacktrace see below.). IMHO Hibernate shouldn't give me an uninitialized value (AbstractPersistentCollection) into the set-method of the PropertyAccessor!? (Hibernate should assume that I want to do something with the collection before setting it to the entity!) If I don't got the concept of a PropertyAccessor completely wrong, the "set"-flow should work like this: 1.) (Hibernate-Core) Load the data from Database (completely*), transform it to a value of the matching type (e.g. List) and hand it over to the PropertyAccessor. 2.) (PropertyAccessor) Receive the (completely*) loaded and transformed value, do whatever modification is necessary and set the result to the entity. completely*: In case of FetchType.EAGER: Completely loaded / in case of FetchType.LAZY: "Ready to access". Correct me if I got something wrong here. BTW: The corresponding List-property of the entity is FetchType.EAGER: {code} @OneToMany (fetch = FetchType.EAGER) @Cascade ({CascadeType.ALL}) @JoinColumn(name="ParentId") @IndexColumn(name="ParentIndex") @AccessType(value="path.to.MyPropertyAccessor") private List<Foo> fooList;{code} (and the Hibernate-session should also be alive at the moment of the Exception) The stacktrace: org.hibernate.LazyInitializationException: illegal access to loading collection at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:363) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108) at org.hibernate.collection.PersistentList.toArray(PersistentList.java:146) at java.util.ArrayList.addAll(ArrayList.java:472) at path.to.MyPropertyAccessor$BasicSetter.set(MyPropertyAccessor.java:71) at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:352) at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:232) at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3580) at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:152) at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877) at org.hibernate.loader.Loader.doQuery(Loader.java:752) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) at org.hibernate.loader.Loader.loadEntity(Loader.java:1885) at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71) at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65) at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3062) at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434) at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415) at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165) at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223) at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126) at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906) at org.hibernate.impl.SessionImpl.get(SessionImpl.java:843) at org.hibernate.impl.SessionImpl.get(SessionImpl.java:836) at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:519) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406) at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512) at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506) // some more...
    via by Stephan Sann,
  • I posted the problem on the Hibernate-forum (https://forum.hibernate.org/viewtopic.php?f=1&t=1003277) and got no answer. So I assume this is a bug. I wrote a custom PropertyAccessor {code} public class MyPropertyAccessor implements PropertyAccessor{code} with this code within the set-Method of the Setter: {code} public void set(Object target, Object value, SessionFactoryImplementor factory) throws HibernateException { try { if (((List) value) == null) { return; } List tempList = new ArrayList(); tempList.addAll((List) value); // ... more code here{code} What I get during runtime is a "LazyInitializationException: illegal access to loading collection" (Stacktrace see below.). IMHO Hibernate shouldn't give me an uninitialized value (AbstractPersistentCollection) into the set-method of the PropertyAccessor!? (Hibernate should assume that I want to do something with the collection before setting it to the entity!) If I don't got the concept of a PropertyAccessor completely wrong, the "set"-flow should work like this: 1.) (Hibernate-Core) Load the data from Database (completely*), transform it to a value of the matching type (e.g. List) and hand it over to the PropertyAccessor. 2.) (PropertyAccessor) Receive the (completely*) loaded and transformed value, do whatever modification is necessary and set the result to the entity. completely*: In case of FetchType.EAGER: Completely loaded / in case of FetchType.LAZY: "Ready to access". Correct me if I got something wrong here. BTW: The corresponding List-property of the entity is FetchType.EAGER: {code} @OneToMany (fetch = FetchType.EAGER) @Cascade ({CascadeType.ALL}) @JoinColumn(name="ParentId") @IndexColumn(name="ParentIndex") @AccessType(value="path.to.MyPropertyAccessor") private List<Foo> fooList;{code} (and the Hibernate-session should also be alive at the moment of the Exception) The stacktrace: org.hibernate.LazyInitializationException: illegal access to loading collection at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:363) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108) at org.hibernate.collection.PersistentList.toArray(PersistentList.java:146) at java.util.ArrayList.addAll(ArrayList.java:472) at path.to.MyPropertyAccessor$BasicSetter.set(MyPropertyAccessor.java:71) at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:352) at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:232) at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3580) at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:152) at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877) at org.hibernate.loader.Loader.doQuery(Loader.java:752) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) at org.hibernate.loader.Loader.loadEntity(Loader.java:1885) at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71) at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65) at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3062) at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434) at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415) at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165) at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223) at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126) at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906) at org.hibernate.impl.SessionImpl.get(SessionImpl.java:843) at org.hibernate.impl.SessionImpl.get(SessionImpl.java:836) at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:519) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406) at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512) at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506) // some more...
    via by Stephan Sann,
  • Lazy Initialization Errors from Refreshes
    via Stack Overflow by Scott
    ,
  • JPA bidirectional relations
    via Stack Overflow by user1291235
    ,
    • org.hibernate.LazyInitializationException: illegal access to loading collection at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:363) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108) at org.hibernate.collection.PersistentList.toArray(PersistentList.java:146) at java.util.ArrayList.addAll(ArrayList.java:472) at path.to.MyPropertyAccessor$BasicSetter.set(MyPropertyAccessor.java:71) at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:352) at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:232) at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3580) at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:152) at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877) at org.hibernate.loader.Loader.doQuery(Loader.java:752) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) at org.hibernate.loader.Loader.loadEntity(Loader.java:1885) at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71) at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65) at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3062) at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434) at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415) at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165) at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223) at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126) at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906) at org.hibernate.impl.SessionImpl.get(SessionImpl.java:843) at org.hibernate.impl.SessionImpl.get(SessionImpl.java:836) at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:519) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406) at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512) at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506)

    Users with the same issue

    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,