Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Very Strange LazyInitializationExceptionsorting entities
PostPosted: Fri Feb 17, 2017 6:30 am 
Newbie

Joined: Fri Feb 17, 2017 5:55 am
Posts: 2
The entity structure is as follows:

Code:
@MappedSuperclass
public abstract class AbstractObject implements Serializable {

    @EmbeddedId
    private ObjectPrimaryKey objectPrimaryKey; // Composite key comprising two Strings and one BigDecimal

    @Column(name = "createdTime", nullable = false)
    private Date createdTime;

    @Column(name = "expiryTime", nullable = false)
    private Date expiryTime;
   
   //Setters and Getters Ommitted for brevity
}

@Entity
public class MainObject extends AbstractObject implements Serializable {

   //Various text fields ommited for brevity

    @Embedded
    private InnerObjectCollection innerObjectcollection;

}


@Embeddable
public class InnerObjectCollection implements Serializable {

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<InnerObject> innerObjects;

}

@Entity
@Access(AccessType.FIELD)
public class InnerObject implements Serializable, Comparable<InnerObject>{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ob_id")
    Long id;

    @Transient
    private AnotherObject object;

   //Various string and date fields

    private int ordinal;
   
    public int compareTo(InnerObject innerObject) {
        return ((Integer)this.ordinal).compareTo(innerObject.getOrdinal());
    }
}


The error occurs in the following method call:

Code:
@Transactional(readOnly = true, propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
private Set<AbstractObject> getSomeObjects() {
   Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
   SQLQuery query = session.createSQLQuery(identityQuerySQL);
   query.setParameter(...);
   query.setParameter(...);
   query.setResultTransformer(Transformers.aliasToBean(resultAlias));
   return getObjectsForIdentities(session, query);
}


private Set<AbstractObjects> getObjectsForIdentities(Session session, SQLQuery query) {
   List<ObjectPrimaryKey> primaryKeys = query.list();

   Set<AbstractObjects> objects = new LinkedHashSet<AbstractObjects>();

   for (ObjectPrimaryKey objectPrimaryKey : primaryKeys) {
      
      AbstractObject obj = (AbstractObject) session.get(forClass, objectPrimaryKey);  //forClass defined else where
      
      objects.add(obj);
   }
   sortObjects(objects);
   return objects;
}

private void sortObjects(Set<AbstractObjects> abstractObjects) {
   for (AbstractObjects obj : abstractObjects) {
      MainObject mObj = (MainObject) obj;
   
      Collections.sort(mObj.getInnerObjectcollection().getInnerObjects());
   }
}


Calling: Collections.sort(mObj.getInnerObjectcollection().getInnerObjects());

results in:

Code:
Caused by: 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.PersistentBag.toArray(PersistentBag.java:280)
   at java.util.Collections.sort(Collections.java:154)
   ... 56 more


The error seems to be in trying to initialise InnerObjectCollection.innerObjects. This is set to FetchType.EAGER though so I don't understand why it would only be initialising when accessed. Surely it should already be loaded?

It's worth saying that the structure and querying above looks a little unauthodox and it is. There are good reasons for the way things are being done which aren't clear as I've greatly simplified the overall code structure.

Can anyone shed any light on why this is occurring? It's only on a small handful of transactions - a few times a week in 1000's of transactions.

The code is managed by an anotation based Spring transaction manager.


Top
 Profile  
 
 Post subject: Re: Very Strange LazyInitializationExceptionsorting entities
PostPosted: Fri Feb 17, 2017 9:14 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1038
Check the stacktrace for the TransactionInterceptor. If you don;t find it, then you have an issue with the Spring transaction management configuration and every query runs in its own transaction, hence the Session is closed after the query has returned its results.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Very Strange LazyInitializationExceptionsorting entities
PostPosted: Fri Feb 17, 2017 12:20 pm 
Newbie

Joined: Fri Feb 17, 2017 5:55 am
Posts: 2
Hi Vlad, thanks for the prompt reply. I can see the TransactionInterceptor in the stack trace:

Caused by: 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.PersistentBag.toArray(PersistentBag.java:280)
at java.util.Collections.sort(Collections.java:154)
at com.myobject.MyObjectUtil.sortObjects(DisruptionDataRepositoryImpl.java:1028)
...
at sun.reflect.GeneratedMethodAccessor123.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)


I am using an older version of Hibernate (via maven):
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.5.3-Final</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.5.3-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.3-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.3-Final</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>


Is there anything else I can try to resolve this issue?

Thanks


Top
 Profile  
 
 Post subject: Re: Very Strange LazyInitializationException sorting entities
PostPosted: Fri Feb 17, 2017 3:52 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1038
Hibernate 3.5.3 is way too old, and we no longer maintain or provide support for it. Try upgrading to 5.x, and see how it works.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.