failed to lazily initialize a collection of role

failed to lazily initialize a collection of role



org.hibernate.LazyInitializationException ?????



I really faced this issue during my project development when I was trying to fix few testing issue. As per the scenario a one-to-many relation from VoiceServiceFileUpload class to VoiceServiceRequest class. When I want to load the VoiceServiceRequests that belongs to a voiceServiceFileUpload , I got this error. 

It seems that the error is caused by Hibernate lazily loading the VoiceServiceRequest  collection i.e. it returns a list of VoiceServiceRequest Ids only to the view. When the view tries to display the data, the session has been closed and hence, the error. Because , by default the FetchType is lazy true. After subsequent investigation and debug , I fix this by using below possible ways.


Error Log :-

ERROR, a7e3d058-4b9a-494a-87a4-08718d397b09: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: au.com.biz.service.sdp.bizservice.vmprovision.scheduler.domain.model.VoiceServiceFileUpload.VoiceServiceRequests, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: au.com.biz.service.sdp.bizservice.vmprovision.scheduler.domain.model.VoiceServiceFileUpload.VoiceServiceRequests, no session or session was closed
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
        at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:411)
        at java.util.HashMap.getEntry(HashMap.java:424)
        at java.util.HashMap.containsKey(HashMap.java:415)
        at java.util.HashSet.contains(HashSet.java:184)
        at org.apache.commons.lang.builder.ToStringStyle.isRegistered(ToStringStyle.java:137)
        at org.apache.commons.lang.builder.ToStringStyle.appendInternal(ToStringStyle.java:421)
        at org.apache.commons.lang.builder.ToStringStyle.append(ToStringStyle.java:395)
        at org.apache.commons.lang.builder.ToStringBuilder.append(ToStringBuilder.java:840)
        at au.com.biz.service.sdp.bizservice.vmprovision.scheduler.domain.model.VoiceServiceFileUpload.toString(VoiceServiceFileUpload.java:68)
        at java.lang.String.valueOf(String.java:2826)
        at java.lang.StringBuffer.append(StringBuffer.java:219)
        at org.apache.commons.lang.builder.ToStringStyle.appendDetail(ToStringStyle.java:545)
        at org.apache.commons.lang.builder.ToStringStyle.appendInternal(ToStringStyle.java:509)
        at org.apache.commons.lang.builder.ToStringStyle.append(ToStringStyle.java:395)
        at org.apache.commons.lang.builder.ToStringBuilder.append(ToStringBuilder.java:840)
        at au.com.biz.service.sdp.bizservice.vmprovision.scheduler.domain.model.VoiceServiceRequest.toString(VoiceServiceRequest.java:106)
        at au.com.biz.service.sdp.bizservice.vmprovision.scheduler.domain.dao.hibernate.HibernateVoiceServiceRequestDao.update(HibernateVoiceServiceRequestDao.java:113)
        at au.com.biz.service.sdp.bizservice.vmprovision.scheduler.facade.spring.SpringVoiceServiceBatchJobFacade.updateRequestProvisionSendDate(SpringVoiceServiceBatchJobFacade.java:180)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        at com.sun.proxy.$Proxy548.updateRequestProvisionSendDate(Unknown Source)
        at au.com.biz.service.sdp.infraservice.scheduler.application.job.VMProvisionBatchJob.executeJob(VMProvisionBatchJob.java:183)
        at au.com.biz.service.core.scheduler.SchedulerJob.executeTrigger(SchedulerJob.java:129)
        at au.com.biz.service.core.scheduler.SchedulerJob.execute(SchedulerJob.java:76)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)


Below the sample configuration for the entity.There is the context configuration , which is missing in this post.The configuration is configured as lazy loading true.
,  

VoiceServiceFileUpload Entity :-

package javadevelopersguide.lab;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="voiceservicefileupload")
public class VoiceServiceFileUpload{

@Id
@Column(name="FILE_ID")
private int FILE_ID;

@Column(name="fileName")
private String fileName;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "voiceServiceFileUpload")
private Set<VoiceServiceRequest> VoiceServiceRequests= new HashSet<VoiceServiceRequest>(0);

/**
* Setter and Getter Method below
*/
}



VoiceServiceRequest Entity :-

package jdevelopersguide.lab;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


@Entity
@Table(name="voiceservicerequest")
public class VoiceServiceRequest{

@Id
@Column
private int ID;

@Column(name="serivcename")
private String serviceName;

@ManyToOne(fetch = FetchType.EAGER, optional = true)
@JoinColumn(name = "FILE_ID", nullable = true)
private VoiceServiceFileUpload voiceServiceFileUpload;

/**
* Setter and Getter Method below
*/

}


Option 1 -

You can use OpenEntityManagerInViewFilter to open your session in view mode. So, you need to configure this in your web.xml file. But, I didnt do this :) . Because I had few more limitation as per my project structure and architecture. 

Add the below code snippet into your web.xml
----------------------------------------------------------

<filter>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


Option 2 -

download file now

Unknown

About Unknown

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :