LazyInitializationException(Spring和Vaadin)尽管有OpenEntityManagerInView

时间:2012-09-10 06:44:18

标签: java spring hibernate jpa vaadin

我使用Spring Roo和Vaadin作为(简单)数据库应用程序。有一个视图显示包含实体的表。对于要显示的表,Hibernate需要延迟加载1:n引用的实体。这适用于小型表,但只要表变大,只有第一部分正确显示,并发生以下异常。

com.vaadin.data.util.MethodProperty$MethodException
    at com.vaadin.data.util.MethodProperty.getValue(MethodProperty.java:612)
    at com.vaadin.data.util.AbstractProperty.toString(AbstractProperty.java:78)
    at com.vaadin.ui.Table.formatPropertyValue(Table.java:3476)
    at eu.wuttke.tinyscrum.ui.dashboard.DashboardTaskStoryTable.formatPropertyValue(DashboardTaskStoryTable.java:66)
    at com.vaadin.ui.Table.getPropertyValue(Table.java:3428)
    at com.vaadin.ui.Table.getVisibleCellsNoCache(Table.java:1853)
    at com.vaadin.ui.Table.refreshRenderedCells(Table.java:1477)
    at com.vaadin.ui.Table.enableContentRefreshing(Table.java:2645)
    at com.vaadin.ui.Table.changeVariables(Table.java:2491)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.changeVariables(AbstractCommunicationManager.java:1455)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1399)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1318)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:763)
    at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:296)
    at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:501)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
    at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
    at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:769)
    at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:698)
    at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:891)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
    at java.lang.Thread.run(Thread.java:679)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.GeneratedMethodAccessor841.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at com.vaadin.data.util.MethodProperty.getValue(MethodProperty.java:610)
    ... 34 more
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:190)
    at eu.wuttke.tinyscrum.domain.Iteration_$$_javassist_1.getName(Iteration_$$_javassist_1.java)
    at eu.wuttke.tinyscrum.domain.Iteration_Roo_JavaBean.ajc$interMethodDispatch1$eu_wuttke_tinyscrum_domain_Iteration_Roo_JavaBean$eu_wuttke_tinyscrum_domain_Iteration$getName(Iteration_Roo_JavaBean.aj)
    at eu.wuttke.tinyscrum.domain.TaskAndStory.getIterationName(TaskAndStory.java:32)
    ... 38 more

我知道来自其他Hibernate应用程序的这个LazyInitializationException异常,它在会话未打开或已经再次关闭时发生。但是看一下堆栈跟踪,似乎OpenEntityManagerInView过滤器被调用了。

有什么想法吗?非常感谢!

1 个答案:

答案 0 :(得分:6)

Vaadin在服务器端具有持久状态。如果将对象绑定到Vaadin组件(例如,通过使用BeanItemContainer),则对象将“生存”在服务器端的多个请求中。

另一方面,Spring(通过OpenEntityManagerInView)将为每个请求创建一个新的EntityManager(EntityManagers不是安全的)并在请求后关闭它。这样,您已经加载的Entities将被分离(EM不再可用),并且当Vaadin向例如新的请求发出新请求时加载更多的表行,延迟加载文件将无法加载(并且您将获得LazyInitializationException),因为加载它们的EM不再存在。

一些解决方案:

  • 使用Eager JPA Fetch(@ManyToOne (fetch= FetchType.EAGER)
  • 使用JOIN FETCH查询加载对象(例如SELECT mag FROM Magazine mag LEFT JOIN FETCH mag.articles WHERE mag.id = 1 See more
  • 使用EclipseLink(EclipseLink将打开一个新的Session来加载分离对象的延迟加载字段)[我们正在使用这种方法]