我有一些已经在运行的 JBoss webapp,还有一个延迟初始化问题。
因此,我被建议在 Spring 中进行调查并使用 OpenEntityManagerInViewFilter
。
不过我还是报错,希望你能帮帮我? 要使用 Spring OEM Filter,我还需要在我的应用程序中进行哪些更改?
我的设置是这样的:
@Entity
class Customer;
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
class DaoService {
@PersistenceContext
EntityManager em;
}
@Named
@RequestScoped
class CustomerFacade;
+jsf 的东西。
[javax.enterprise.resource.webcontainer.jsf.context] (http--127.0.0.1-8080-1) org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: domain.Customer.customerList, no session or session was closed
我是这样设置的: 网页.xml
<filter>
<filter-name>
OpenEntityManagerInViewFilter
</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<!-- Include this if you are using Hibernate -->
<filter-mapping>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring config -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
应用程序上下文.xml:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.JBossTransactionManagerLookup
</prop>
</props>
</property>
</bean>
持久性.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, the example data source is just for proofs of concept! -->
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
这基本上就是所有的设置。除了延迟加载问题之外,一切正常。
最佳答案
有必要在EntityManager的初始化参数中注明
<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>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
关于java - 使用 OpenEntityManagerInViewFilter 进行延迟初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9304020/