Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy Initialisation with OpenEntityManagerInViewFilter?

I have a little already working JBoss webapp, and a Lazy initialisation problem. Therefore I was advised to investigate in Spring and use OpenEntityManagerInViewFilter.

Nevertheless I still get the error, hope you could help me? What else do I have to change in my app to make use of the Spring OEM Filter?

My setup is like this:

@Entity
class Customer;

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
class DaoService {
  @PersistenceContext
  EntityManager em;
}

@Named
@RequestScoped
class CustomerFacade;

+jsf stuff.

[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

I set it up like this: web.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>

applicationContext.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>

persistence.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>

Thats basically all the setup. And everything is working apart from the lazy loading problem.

like image 937
membersound Avatar asked Feb 16 '12 00:02

membersound


2 Answers

Is necessary to indicate in the init parameters 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>
like image 136
Jhonathan Avatar answered Oct 17 '22 00:10

Jhonathan


Make sure you don't access old entities (for example serialized ones in the session in previous request).

like image 22
vinga Avatar answered Oct 17 '22 01:10

vinga