I'm trying to get the list of Review of a Movie, when I'm trying to use the FetchType Lazy on Review class It causes LazyInitializationException , When I'm trying @ManyToOne(fetch = FetchType.EAGER) the query is slow and takes time to return results
@Entity
@Table(name = "REVIEW")
public class Review implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "REVIEW_SEQ")
@SequenceGenerator(name = "REVIEW_SEQ", sequenceName = "REVIEW_SEQ", allocationSize = 1)
@Column(name = "ID", length = 15)
private Long id;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "FK_ID_MOVIE", referencedColumnName = "ID_Movie", nullable = false)
private Movie refMovie;
//Other properties
}
@Entity
@Table(name = "MOVIE")
public class Movie implements Serializable {
private static final long serialVersionUID = 1L;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "FK_ID_MOVIE", referencedColumnName = "ID_MOVIE", nullable = true, insertable = true, updatable = true)
private List<Review> refReview;
It causes :
[org.apache.cxf.phase.PhaseInterceptorChain] (default task-1) Application has thrown exception, unwinding now: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
14:01:49,323 INFO [org.apache.cxf.phase.PhaseInterceptorChain] (default task-1) Application has thrown exception, unwinding now: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
14:01:49,324 WARNING [org.apache.cxf.phase.PhaseInterceptorChain] (default task-1) Exception in handleFault on interceptor org.apache.cxf.binding.xml.interceptor.XMLFaultOutInterceptor@108dab92: org.apache.cxf.interceptor.Fault: could not initialize proxy - no Session
at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:148)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:114)
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:130)
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:82)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:98)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:236)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:104)
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:191)
at ma.com.manager.movie.model.Movie$$_jvst1bf_21.getId(Movie$$_jvst1bf_21.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)...
I solved this issue by declaring both entities as fetch = FetchType.LAZY
, and using hibernate initializer :
Movie mv = review.getRefMovie();
Hibernate.initialize(mv);