I'm looking for the cleanest way to inject (or otherwise best initialize) JDO PersistenceManager
and/or PersistenceManagerFactory
to RESTful web application resources based on JAX-RS / Jersey 2 and EJB 3.1 Lite, preferably without adding too many extra dependencies. The project uses DataNucleus and is built with Maven 3.
Here are some pointers that I found. Maybe you've already tried some of these and found which approach works best:
- Writing a
Provider<PersistenceManager>
- Writing a Servlet
Filter
- http://www.datanucleus.org/products/datanucleus/jdo/pmf.html
- http://blog.yanivkessler.com/2010/06/lightweight-jdo-persistence-filter.html
- How to use JDO persistence manager?
- Google Guice
The current solution is based on JPA and the injection works as usual. The following code has been simplified and unfortunately I can't post the original.
In main/java/project/ws/rs/TestResource.java
:
@Path("/test")
@Stateless
public class TestResource {
@PersistenceContext(unitName = "project-webapi")
private EntityManager em;
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response test(@PathParam("id") UUID id) {
return Response.status(
Response.Status.OK).entity(em.find(type, id)).build();
}
}
As you can see above, the persistent JAX-RS resources are annotated as @Stateless
EJB's.
All the Java EE and Jersey dependencies are provided by GlassFish 4.0 that is my target platform. DataNucleus libraries are included in the runtime deployable war artifact in WEB-INF/lib
.
The project has a standard persistence unit described by the main/resources/META-INF/persistence.xml
file.
<persistence>
<persistence-unit name="project-webapi"/>
</persistence>
(XML namespaces and schema references have been omitted for simplicity.)
The deployment descriptor is in main/webapp/WEB-INF/web.xml
:
<web-app version="3.0" metadata-complete="false" />
The goal
I would like to switch from JPA to JDO without losing clean dependency injection like above. An ideal solution would of course be similar to just replacing the EntityManager
with PersistenceManager
above, but how to achieve that or perhaps there are better ways? It doesn't have to be injection, if some other way is more efficient for this purpose.
The reason I'm switching is to be able to use also non-SQL persistence with the help of DataNucleus and to have a full ORM implementation available to my Java EE web applications.
I'm sure there are many others who would be interested in this. Any ideas?
Edit: Finding the most efficient way for web apps like above to obtain a reference to a PersistenceManager
is the point of this question.
With JPA it's done by injection. Obviously we know that the Java EE spec and application servers do not support this directly with JDO. Otherwise we wouldn't be asking this. So we are after the cleanest and the "most deployable" way to do it for web apps.