Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't get OpenEntityManagerInViewFilter to work in JBoss 6.1
    primarykey
    data
    text
    <p>I am trying to add open-session-in-view behavior to an existing pure JPA application. Using Spring in the service-tier is not an option. I would like to wrap the view in Spring's OpenEntityManagerInViewFilter, and not have to modify the EJB layer.</p> <p>I am not having any luck getting OpenEntityManagerInViewFilter (Spring 3.2.2) to work in JBoss 6.1. The filter is definitely being invoked, but I am still getting a LazyInitializationException in the view.</p> <p>The filter and the session-bean are using a different instance (and class) of the EntityManager. The filter is getting a <code>org.hibernate.ejb.EntityManagerImpl</code>, while the session-bean is getting a <code>org.jboss.jpa.tx.TransactionScopedEntityManager</code>. I am not sure what Spring configuration is responsible for this.</p> <p>Here is the relevant code/config:</p> <p><em><strong>war/WEB-INF/classes/test.web.servlet.TestServlet</em></strong></p> <pre><code>public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; @EJB private ServiceLocal service; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { long parentId = Long.parseLong(req.getParameter("parentId")); Parent parent = service.retrieveParent(parentId); // this call throws a LazyInitializationException // because parent.children.session is NULL parent.getChildren().iterator().next().getName(); req.setAttribute("parent", parent); RequestDispatcher requestDispatcher = this.getServletContext().getRequestDispatcher("/WEB-INF/jsp/view.jsp"); requestDispatcher.forward(req, resp); } } </code></pre> <p><em><strong>ejb/test.ejb.session.ServiceBean</em></strong></p> <pre><code>@Stateless @TransactionAttribute(TransactionAttributeType.REQUIRED) public class ServiceBean implements ServiceLocal, Service { @PersistenceContext(name="test") private EntityManager entityManager; @Override public Parent retrieveParent(Long parentId) { return entityManager.find(Parent.class, parentId); } } </code></pre> <p><em><strong>war/WEB-INF/web.xml</em></strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt; &lt;display-name&gt;test-war&lt;/display-name&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/spring.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;filter&gt; &lt;filter-name&gt;osiv-filter&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;flushMode&lt;/param-name&gt; &lt;param-value&gt;AUTO&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;servlet&gt; &lt;servlet-name&gt;test-servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;test.web.servlet.TestServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;test-servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;*.do&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;osiv-filter&lt;/filter-name&gt; &lt;servlet-name&gt;test-servlet&lt;/servlet-name&gt; &lt;/filter-mapping&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt; </code></pre> <p><em><strong>war/WEB-INF/spring.xml</em></strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt; &lt;beans&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt; &lt;/property&gt; &lt;property name="persistenceUnitName" value="test" /&gt; &lt;property name="jpaProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.transaction.manager_lookup_class"&gt; org.hibernate.transaction.JBossTransactionManagerLookup &lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p><em><strong>ejb/META-INF/persistence.xml</em></strong></p> <pre><code>&lt;persistence 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_1_0.xsd" version="1.0"&gt; &lt;persistence-unit name="test" transaction-type="JTA"&gt; &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt; &lt;jta-data-source&gt;java:/MSSQLDS&lt;/jta-data-source&gt; &lt;properties&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/&gt; &lt;property name="hibernate.show_sql" value="false" /&gt; &lt;property name="hibernate.format_sql" value="true" /&gt; &lt;property name="hibernate.use_sql_comments" value="true" /&gt; &lt;property name="jboss.entity.manager.factory.jndi.name" value="java:/testEntityManagerFactory" /&gt; &lt;property name="jboss.entity.manager.jndi.name" value="java:/testEntityManager" /&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload