Friday, 29 May 2015

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 Id's 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 didn't 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 -

This issue is happening the ,because the session has closed before you load the collection.So, you can open a new session, but its not suggested.So, I didn't do this :)

After gooooogle almost few articles , I found you can also try @Transactional for that method.Because,it makes your session active. But, I didn't try this. But , you can try your luck :)



Option 3 -

Finally , I have configured the VoiceServiceFileUpload to load eager (i.e fetch = FetchType.EAGER) and it resolved my issue.

Working fix :-

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


Hope this will help you and will save your all day :::)

Follow for more details on Google+ and @Facebook!!!

Find More Issues & Solutions -

Thursday, 28 May 2015

How to convert epoch to human readable format in UNIX

How to convert epoch to human readable format in UNIX ?


In this example the epoch time is 1432783870. Use the below command to convert the epoch time to human readable time.Technically epoch time known as UNIX time , also known as POSIX time. 

Example :-

dev@javadevelopersguide-developer-desktop ~/Epoch/ $ date -d @1432783870
Thu May 28 13:31:10 EST 2015
dev@javadevelopersguide-developer-desktop ~/Epoch/ $ date -d @1377360000
Sun Aug 25 02:00:00 EST 2013

Find more here.




Follow for more details on Google+ and @Facebook!!!

Find More :-