Ryan has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

How to use GlassFish Managed JPA EntityManager in Spring

  • submit to reddit

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:



 Next, add the following configuration to your Spring XML config file:

 <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" >  
<property name="persistenceUnits">
<entry key="MyPU1" value="persistence/MyPU1"/>
<entry key="MyPU2" value="persistence/MyPU2"/>


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

Your rating: None Average: 4 (1 vote)
Published at DZone with permission of its author, Ryan Developer.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Reza Rahman replied on Tue, 2009/08/11 - 8:58pm

Good job Ryan...



Ryan Developer replied on Tue, 2009/08/11 - 10:35pm

Thank you Reza!  Once I get some EJB 3.1 + JCDI experience I plan to write about them too.  I'm not sure what I'll write about yet since it's all pre-integrated and doesn't need to be configured.

Chris Riddick replied on Wed, 2009/08/12 - 6:17am

How would you override the...

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" > 

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

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

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:


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,


Ryan Developer replied on Wed, 2009/08/12 - 12:24pm

Excellent suggestions Reza, thank you.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.