我使用JSF Primefaces,Spring,Spring Data JPA和JPA与Hibernate提供程序。
我想在我的1:N关系中使用OSIV模式,我不希望EAGER既不能在访问数据库的服务方法中自己获取实体。
所以我使用OpenEntityManagerInViewFilter。查询数据库的My Spring服务方法是@Transactional annotated。
我认为我的配置没问题,但我得到了Hibernate的LazyInitializationException。
我在互联网上搜索了很多但没找到原因。 ¿任何人都可以帮助我吗?感谢。
我得到的例外日志:
[24 abr 2014 - 21:49:34] DEBUG (OpenEntityManagerInViewFilter:160) - Opening JPA EntityManager in OpenEntityManagerInViewFilter
[24 abr 2014 - 21:49:34] INFO (FaseListener:22) - BEFORE RESTORE_VIEW 1
[24 abr 2014 - 21:49:34] INFO (FaseListener:25) - view root null
[24 abr 2014 - 21:49:34] INFO (FaseListener:18) - AFTER RESTORE_VIEW 1
[24 abr 2014 - 21:49:34] INFO (FaseListener:22) - BEFORE APPLY_REQUEST_VALUES 2
[24 abr 2014 - 21:49:34] INFO (FaseListener:27) - view id = /paginas/categoria/categoriaCrud.xhtml
[24 abr 2014 - 21:49:34] INFO (FaseListener:18) - AFTER APPLY_REQUEST_VALUES 2
[24 abr 2014 - 21:49:34] INFO (FaseListener:22) - BEFORE PROCESS_VALIDATIONS 3
[24 abr 2014 - 21:49:34] INFO (FaseListener:27) - view id = /paginas/categoria/categoriaCrud.xhtml
[24 abr 2014 - 21:49:34] INFO (FaseListener:18) - AFTER PROCESS_VALIDATIONS 3
[24 abr 2014 - 21:49:34] INFO (FaseListener:22) - BEFORE UPDATE_MODEL_VALUES 4
[24 abr 2014 - 21:49:34] INFO (FaseListener:27) - view id = /paginas/categoria/categoriaCrud.xhtml
[24 abr 2014 - 21:49:34] INFO (FaseListener:18) - AFTER UPDATE_MODEL_VALUES 4
[24 abr 2014 - 21:49:34] INFO (FaseListener:22) - BEFORE INVOKE_APPLICATION 5
[24 abr 2014 - 21:49:34] INFO (FaseListener:27) - view id = /paginas/categoria/categoriaCrud.xhtml
[24 abr 2014 - 21:49:34] INFO (CategoriaController:85) - Método editarAction
[24 abr 2014 - 21:49:34] INFO (FaseListener:18) - AFTER INVOKE_APPLICATION 5
[24 abr 2014 - 21:49:34] INFO (FaseListener:22) - BEFORE RENDER_RESPONSE 6
[24 abr 2014 - 21:49:34] INFO (FaseListener:27) - view id = /paginas/categoria/categoriaCrud.xhtml
abr 24, 2014 9:49:34 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
SEVERE: Error Rendering View[/paginas/categoria/categoriaCrud.xhtml]
javax.el.ELException: /paginas/categoria/categoriaForm.xhtml @41,21 value="#{categoriaController.listaClientesCategoria}": Error reading 'listaClientesCategoria' en el tipo controladores.CategoriaController
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIData.getValue(UIData.java:732)
at org.primefaces.component.datatable.DataTable.getValue(DataTable.java:867)
at org.primefaces.component.api.UIData.getDataModel(UIData.java:579)
at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:720)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:79)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at org.primefaces.component.tabview.TabViewRenderer.encodeTabContent(TabViewRenderer.java:279)
at org.primefaces.component.tabview.TabViewRenderer.encodeContents(TabViewRenderer.java:237)
at org.primefaces.component.tabview.TabViewRenderer.encodeMarkup(TabViewRenderer.java:127)
at org.primefaces.component.tabview.TabViewRenderer.encodeEnd(TabViewRenderer.java:71)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at org.primefaces.component.panelgrid.PanelGridRenderer.encodeDynamicBody(PanelGridRenderer.java:92)
at org.primefaces.component.panelgrid.PanelGridRenderer.encodeBody(PanelGridRenderer.java:60)
at org.primefaces.component.panelgrid.PanelGridRenderer.encodeEnd(PanelGridRenderer.java:49)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:115)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:59)
at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:47)
at org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:51)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:177)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Caused by: javax.el.ELException: Error reading 'listaClientesCategoria' en el tipo controladores.CategoriaController
at javax.el.BeanELResolver.getValue(BeanELResolver.java:98)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
... 63 more
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: modelo.entidades.Categoria.clientes, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:572)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:212)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:551)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:140)
at org.hibernate.collection.internal.PersistentSet.toArray(PersistentSet.java:187)
at java.util.ArrayList.<init>(ArrayList.java:151)
at controladores.CategoriaController.getListaClientesCategoria(CategoriaController.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
... 68 more
[24 abr 2014 - 21:49:34] INFO (FaseListener:18) - AFTER RENDER_RESPONSE 6
[24 abr 2014 - 21:49:34] DEBUG (OpenEntityManagerInViewFilter:185) - Closing JPA EntityManager in OpenEntityManagerInViewFilter
[24 abr 2014 - 21:49:34] DEBUG (EntityManagerFactoryUtils:435) - Closing JPA EntityManager
abr 24, 2014 9:49:34 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: El Servlet.service() para el servlet [Faces Servlet] en el contexto con ruta [/ejemplo] lanzó la excepción [failed to lazily initialize a collection of role: modelo.entidades.Categoria.clientes, could not initialize proxy - no Session] con causa raíz
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: modelo.entidades.Categoria.clientes, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:572)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:212)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:551)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:140)
at org.hibernate.collection.internal.PersistentSet.toArray(PersistentSet.java:187)
at java.util.ArrayList.<init>(ArrayList.java:151)
at controladores.CategoriaController.getListaClientesCategoria(CategoriaController.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIData.getValue(UIData.java:732)
at org.primefaces.component.datatable.DataTable.getValue(DataTable.java:867)
at org.primefaces.component.api.UIData.getDataModel(UIData.java:579)
at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:720)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:79)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at org.primefaces.component.tabview.TabViewRenderer.encodeTabContent(TabViewRenderer.java:279)
at org.primefaces.component.tabview.TabViewRenderer.encodeContents(TabViewRenderer.java:237)
at org.primefaces.component.tabview.TabViewRenderer.encodeMarkup(TabViewRenderer.java:127)
at org.primefaces.component.tabview.TabViewRenderer.encodeEnd(TabViewRenderer.java:71)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at org.primefaces.component.panelgrid.PanelGridRenderer.encodeDynamicBody(PanelGridRenderer.java:92)
at org.primefaces.component.panelgrid.PanelGridRenderer.encodeBody(PanelGridRenderer.java:60)
at org.primefaces.component.panelgrid.PanelGridRenderer.encodeEnd(PanelGridRenderer.java:49)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:115)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:59)
at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:47)
at org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:51)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:177)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
[24 abr 2014 - 21:49:34] INFO (FaseListener:22) - BEFORE RESTORE_VIEW 1
[24 abr 2014 - 21:49:34] INFO (FaseListener:25) - view root null
[24 abr 2014 - 21:49:34] INFO (FaseListener:18) - AFTER RESTORE_VIEW 1
[24 abr 2014 - 21:49:34] INFO (FaseListener:22) - BEFORE RENDER_RESPONSE 6
[24 abr 2014 - 21:49:34] INFO (FaseListener:27) - view id = /paginas/error/errorServidor.xhtml
[24 abr 2014 - 21:49:34] INFO (FaseListener:18) - AFTER RENDER_RESPONSE 6
[24 abr 2014 - 21:49:34] DEBUG (OpenEntityManagerInViewFilter:160) - Opening JPA EntityManager in OpenEntityManagerInViewFilter
[24 abr 2014 - 21:49:34] DEBUG (OpenEntityManagerInViewFilter:185) - Closing JPA EntityManager in OpenEntityManagerInViewFilter
[24 abr 2014 - 21:49:34] DEBUG (EntityManagerFactoryUtils:435) - Closing JPA EntityManager
相关配置文件:
web.xml(OEMIV过滤器是我拥有的所有过滤器中的第一个):
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/spring-config.xml
classpath:/springDataJpa-config.xml
</param-value>
</context-param>
...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...
<filter>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>entityManagerFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Spring配置文件:
<context:annotation-config/>
<context:component-scan base-package="modelo"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean"
p:jndiName="jdbc/conexionBBDD"
p:resourceRef="true" />
<bean id="dataSourceProxy"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
p:targetDataSource-ref="dataSource"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSourceProxy"
p:packagesToScan="modelo.entidades"
p:jpaVendorAdapter-ref="jpaHibernateAdapter"
p:jpaDialect-ref="jpaHibernateDialect"
p:jpaProperties-ref="jpaHibernateProperties" />
<bean id="jpaHibernateAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="ORACLE"
p:databasePlatform="org.hibernate.dialect.Oracle10gDialect"
p:showSql="true" />
<bean id="jpaHibernateDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<util:properties id="jpaHibernateProperties" location="classpath:jpa-hib.properties" />
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<tx:annotation-driven transaction-manager="transactionManager" />
我的spring数据jpa配置文件:
<jpa:repositories base-package="modelo.daos" />