Announcement Announcement Module
Collapse
No announcement yet.
OpenEntityManagerInViewFilter with JPA PersistenceAnnotationBeanPostProcessor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenEntityManagerInViewFilter with JPA PersistenceAnnotationBeanPostProcessor

    Hi, I am trying to configure org.springframework.orm.jpa.support.OpenEntityMana gerInViewFilter in my web.xml. I'm using Spring 3.0.5.RELEASE

    I'm using PersistenceAnnotationBeanPostProcessor with JPA and JTA with annotations on my DAO classes, e.g.

    Code:
    @PersistenceContext(unitName = "onePU")
    private EntityManager em;
    and my spring database configuration looks like;

    Code:
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
        <property name="persistenceUnits">
          <map>
            <entry key="onePU" value="persistence/onePU" />
            <entry key="twoPU" value="persistence/twoPU" />
            <entry key="threePU" value="persistence/threePU" />
          </map>
        </property>
      </bean>
      <tx:annotation-driven />
      <tx:jta-transaction-manager />
    I've got a single JPA 2.0 persistence.xml with the multiple persistence units defined in it. Its running in Glassfish 3.

    Everything has worked just fine with this set up until I try to configure the OpenEntityManagerInViewFilter in the web.xml but it instantly complains that there's no bean called "entityManagerFactory" (of course not, it's coming from the JTA container, isn't it?). I tried configuring one manually and injecting with one of my persistence units but it doesn't seem to be opened when the view hits it as I get the session expiry message in the bit of web-layer code I'm trying to invoke (at the moment I only need it for one persistenceUnit but I don't mind configuring one ViewFilter per persistence unit if I have to later).

    Looking at the Javadoc/source code for the PersistenceAnnotationBeanPostProcessor it seems to imply that it will create/access one EntityManagerFactory per persistentUnit it is injected with. What would be the name of this EntityManagerFactory? I thought it might be named after the Persistent Unit itself (e.g. "onePU") but that doesn't seem to work either, as per below;

    [CODE]
    <filter>
    <filter-name>openEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEnti tyManagerInViewFilter</filter-class>
    <init-param>
    <param-name>entityManagerFactoryBeanName</param-name>
    <param-value>onePU</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>openEntityManagerInViewFilter</filter-name>
    <url-pattern>/one/*</url-pattern>
    </filter-mapping>
    [CODE]

    However in this configuration it just complains that there is not entitymanagerfactory called "onePU".

    Anyone have any ideas, tips or prior experience with this?

    thanks muchly for any help,

  • #2
    I resolved this issue myself with some careful re-reading of the JavaDoc for the PersistenceAnnotationBeanPostProcessor.

    I removed the persistenceUnits configuration items from the PersistenceAnnotationBeanPostProcessor and put a JNDI lookup for each of the persistence units as follows;

    Code:
      <tx:annotation-driven />
      <tx:jta-transaction-manager />
    
      <bean id="pabpp" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    
      <jee:jndi-lookup id="onePU" jndi-name="persistence/onePU" />
      <jee:jndi-lookup id="twoPU" jndi-name="persistence/twoPU" />
      <jee:jndi-lookup id="threePU" jndi-name="persistence/threePU" />
    Then in my web.xml I configured the OpenEntityManagerInViewFilter as normal with reference to the id from the JNDI look up as above;

    Code:
      <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>onePU</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>openEntityManagerInViewFilter</filter-name>
        <url-pattern>/one/*</url-pattern>
      </filter-mapping>
    I have now successfully got access to lazily loaded members of my persistant classes outside the transaction boundaries of my services; with JPA annotations, JTA transaction mgr from my container and multiple persistence units.

    I feel this type of complex configuration ought to be documented somewhere in the Spring Docs! It has taken a lot of reading between the lines of various Spring Docs and JavaDocs and multiple chunks of code inspection and dozens of blogs read and so forth to get this far.

    Comment

    Working...
    X