2010年2月22日星期一

Spring + JPA in Google App Engine


I've been playing around with Google App Engine because it's new and cool and has some interesting possibilities. However, it's in a preview mode currently and has some limitations, as expected.

One difficulty I encountered was getting Spring (2.5.6) to work nicely with the App Engine-provided JPA. But now I have it working. Here's how.

The basic problem is that PersistenceAnnotationBeanPostProcessor has a dependency on javax.naming. But that's the thing you need to get @PersistenceContext to work. And you want that to work because you want to be able to have transactions that span DAOs (or, at least, I wanted that).

That dependency is easy to resolve, however. Just grab the source to PersistenceAnnotationBeanPostProcessor, put it in your own package, and replace the catch blocks with NamingException with just Exception … you won't be using those things anyway.

Now, use that bean in your context configuration file.
<bean id="persistienceAnnotationBeanPostProcessor"
class="your.package.name.PersistenceAnnotationBeanPostProcessor" />

Finally, use the @PersistenceContext annotation in your DAO just like it says in the Spring documentation:
@PersistenceContext
private EntityManager entityManager;

You're done. Thank me.

Update 5/25: As Alex points out I spelled the bean ID incorrectly. However, that was right from my code, so the id doesn't really matter. In fact, now I have:
<bean class="your.package.name.PersistenceAnnotationBeanPostProcessor" />

没有评论:

发表评论