org.hibernate.LazyInitializationException: illegal access to loading collection

Hibernate JIRA | Stephan Sann | 7 years ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    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...

    Hibernate JIRA | 7 years ago | Stephan Sann
    org.hibernate.LazyInitializationException: illegal access to loading collection
  2. 0

    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...

    Hibernate JIRA | 7 years ago | Stephan Sann
    org.hibernate.LazyInitializationException: illegal access to loading collection

    Root Cause Analysis

    1. org.hibernate.LazyInitializationException

      illegal access to loading collection

      at org.hibernate.collection.AbstractPersistentCollection.initialize()
    2. Hibernate
      PersistentList.toArray
      1. org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:363)
      2. org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
      3. org.hibernate.collection.PersistentList.toArray(PersistentList.java:146)
      3 frames
    3. Java RT
      ArrayList.addAll
      1. java.util.ArrayList.addAll(ArrayList.java:472)
      1 frame
    4. path.to
      MyPropertyAccessor$BasicSetter.set
      1. path.to.MyPropertyAccessor$BasicSetter.set(MyPropertyAccessor.java:71)
      1 frame
    5. Hibernate
      AbstractEntityPersister.load
      1. org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:352)
      2. org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:232)
      3. org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3580)
      4. org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:152)
      5. org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
      6. org.hibernate.loader.Loader.doQuery(Loader.java:752)
      7. org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
      8. org.hibernate.loader.Loader.loadEntity(Loader.java:1885)
      9. org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
      10. org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
      11. org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3062)
      11 frames
    6. Hibernate
      SessionImpl.get
      1. org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
      2. org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
      3. org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
      4. org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
      5. org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
      6. org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906)
      7. org.hibernate.impl.SessionImpl.get(SessionImpl.java:843)
      8. org.hibernate.impl.SessionImpl.get(SessionImpl.java:836)
      8 frames
    7. Hibernate
      HibernateTemplate.get
      1. org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:519)
      2. org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
      3. org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
      4. org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512)
      5. org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506)
      5 frames