How to use GlassFish Managed JPA EntityManager in Spring
When you deploy your application into a Java EE 5 application server it detects the persistence.xml, creates an EntityManager for each persistence unit, and exposes them in JNDI. You can get Spring to load the EntityManagers of all your persistence units from JNDI. First you need to tell web.xml that you want to load the persistence unit references from JNDI. The JNDI name always starts with "persistence/" and ends with the persistence unit name. For example:
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/MyPU1</persistence-unit-ref-name>
<persistence-unit-name>MyPU1</persistence-unit-name>
</persistence-unit-ref>
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/MyPU2</persistence-unit-ref-name>
<persistence-unit-name>MyPU2</persistence-unit-name>
</persistence-unit-ref>
Next, add the following configuration to your Spring XML config file:
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" >
<property name="persistenceUnits">
<map>
<entry key="MyPU1" value="persistence/MyPU1"/>
<entry key="MyPU2" value="persistence/MyPU2"/>
</map>
</property>
</bean>
<tx:jta-transaction-manager/>
<tx:annotation-driven/>
Spring is now configured to use the JPA EntityManagers and JTA transactions from GlassFish. Use persistence.xml, @PersistenceUnit, and @PersistenceContext like normal. Use Spring's proprietary @Transactional annotation for transaction demarcation. If you need to query both persistence units within the same transaction then you need to use XA data sources.
Originally posted on ryandelaplante.com
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
Reza Rahman replied on Tue, 2009/08/11 - 8:58pm
Good job Ryan...
Cheers,
Reza
Ryan De Laplante replied on Tue, 2009/08/11 - 10:35pm
Chris Riddick replied on Wed, 2009/08/12 - 6:17am
How would you override the...
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" >
...
</bean>
...for a unit test, or is there a way of setting up a mock JNDI context that the persistence unit can be looked up from.
Chris Riddick replied on Wed, 2009/08/12 - 6:55am
I worked out a solution. By adding an ID to the bean e.g
<bean id="pabpp" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" >
...
</bean>
you can then override it in a test-ApplicationContext e.g
<!-- Bean overrides for ApplicationContext.xml -->
<bean id="pabpp" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
My test loads the contexts like so...
@Override
protected String[] getConfigLocations() {
return new String[] { "classpath*:applicationContext.xml", "classpath*:test-applicationContext.xml" };
}
There may be a better way but this is pretty clean.
Reza Rahman replied on Wed, 2009/08/12 - 12:12pm
in response to:
Ryan De Laplante
Ryan,
How about an analysis piece covering the pros vs. the cons of Java EE 6 with/without Spring? I am giving a talk on a very similar topic for JBossWorld this year, feel free to check it out. Also, how about a analysis of the current state of OSGi? I think the community could benefit from an honest analysis from someone without anything to "sell" but something valuable to say. I was planning on doing something similar, but don't really have the time. You might find this interesting: http://blog.caucho.com/?tag=osgi. Also, you could write an article about contributing to the community at large as a developer via open source, standards, writing, speaking etc.
All the best,
Reza
Ryan De Laplante replied on Wed, 2009/08/12 - 12:24pm