为什么 EAGER 需要时间才能得到结果而 LAZY 会导致异常?
Why EAGER takes time to get result and LAZY causes exception?
我正在尝试获取电影评论列表,当我尝试使用 FetchType Lazy on Review class 它导致 LazyInitializationException ,当我尝试 @ManyToOne(fetch = FetchType.EAGER) 查询很慢并且需要时间才能得到 return 结果
@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;
它导致:
[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.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)...
我通过将两个实体都声明为 fetch = FetchType.LAZY
并使用休眠初始化程序解决了这个问题:
Movie mv = review.getRefMovie();
Hibernate.initialize(mv);
我正在尝试获取电影评论列表,当我尝试使用 FetchType Lazy on Review class 它导致 LazyInitializationException ,当我尝试 @ManyToOne(fetch = FetchType.EAGER) 查询很慢并且需要时间才能得到 return 结果
@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;
它导致:
[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.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)...
我通过将两个实体都声明为 fetch = FetchType.LAZY
并使用休眠初始化程序解决了这个问题:
Movie mv = review.getRefMovie();
Hibernate.initialize(mv);