The Aries JPA project will make it easy for JPA persistence providers such as Apache OpenJPA to be used in an OSGi environment and will provide container managed persistence for the Blueprint container.

For more information, check out section "127 JPA Service Specification Version 1.0" in the "OSGi Service Platform Enterprise Specification, Release 4, Version 4.2" available for public download from the OSGi Alliance .

Developing an Aries JPA project is very easy and can be achieved with simple steps described in the "Creation of a JPA project using Maven" section. First, however it is useful to understand some of the basic concepts.

Aries JPA provides container managed JPA support in an OSGi framework.

Persistence bundles

A bundle is regarded as a persistence bundle if it contains the header Meta-Persistence: in it's Manifest. The value of the Meta-Persistence: header is a comma separated list of locations where JPA persistence descriptors can be found. If the header value is empty then a default of META-INF/persistence.xml is used.

For example:


means that META-INF/persistence.xml will be searched for. For non standard locations:

Meta-Persistence: persistence/myPu.xml, pUnit.jar!/someFolder/anotherPu.xml

means that the locations "persistence/myPu.xml" (relative to the root of the bundle), and "someFolder/anotherPu.xml" (relative to the root of pUnit.jar, which is in the root of the bundle) will be searched.

The persistence bundle typically also contains the JPA entities.

The Aries JPA 2 modules

Aries JPA consists of four bundles.

Aries JPA container (org.apache.aries.jpa.container)

The Aries JPA container bundle implements the OSGi JPA service specification.

It tracks persistence unit bundles and creates an EntityManagerFactory service as soon as all dependencies are met.

The EntityManagerFactory services will be registered with the following properties: - this is the name of the persistence unit
osgi.unit.provider - this is the class name of the JPA PersistenceProvider that was used to create the EntityManagerFactory
org.apache.aries.jpa.container.managed - this property will be set to true, indicating this is a managed EntityManagerFactory.

For each persistence unit jpa container first determines which persistence provider to use by analyzing the "provider" property of persistence.xml. It will track a PersistenceProvider service that matches this name.

The next step is to configure a DataSource. The spec defines that the database properties are used to determine which DataSourceFactory service to use and create a DataSource from it.

Additionally aries jpa supports refering to a DataSource service using the jta-datasource or non-jta-datasource properties. The syntax is the aries jndi syntax to search for services.

If using a JTA persistence unit keep in mind that you still have to supply a javax.sql.DataSource not an XADataSource. This DataSource must wrap an XADataSource and provide XA resource enlistment. The simplest way to achieve this is to use pax-jdbc.

As soon as PersistenceProvider and DataSource are available the EntityManagerFactory service is created. Aries JPA container also supports classpath scanning and load time weaving of JPA entities.

Useful notes:

  • You should never call close on the EntityManagerFactory service. This call will be made by the container when the persistence bundle is removed or refreshed. If you do close the EntityManagerFactory service then it will be closed for all users of the service.

Aries JPA container is a standalone bundle with no other dependencies.

Aries JPA API (org.apache.aries.jpa.api)

A set of interfaces to make it easier to use JPA in OSGi. It contains two main interfaces:

  • EmSupplier: Allows to get a thread safe EntityManager and mark entry and exit of blocks that access the EntityManager. This is rather low level and meant to be used mainly by frameworks.
  • JpaTemplate: Allows to write closures that can safely access an EntityManager and are executed inside a transaction.

Aries JPA support(

For each EntityManagerFactory service this bundle provides additional EmSuppler and JpaTemplate services.

Aries JPA blueprint extension (org.apache.aries.jpa.blueprint)

Provides a blueprint extension for @PersistenceUnit and @PersistenceContext injection. To use the extension add this namespace to your blueprint xmlns:jpa="" and enable annotation support using the element on top level. For more details see the aries jpa blueprint example:

Creation of a JPA project using Maven

The first step consist in to create a maven module and make the following modifications to allow to deploy it as OSGI bundle on the platform and reference where the persistence XML file must loaded by the classpath to allow to the JPA container to configure the project accordingly.

Step 1 : Create a bundle

OSGi bundles are mostly regular jars but they need to contain some special OSGi headers in the Manifest. The two changes make sure your maven project creates a valid OSGi bundle.


and that you must configure the maven-bundle-plugin ( to generate the MANIFEST.MF file required by OSGI platform.

          <!-- Only needed for the persistence bundle containing the jpa Entities -->
      <!-- Needed for runtime enhancement when using hibernate -->
          <Dynamic-Import-Package>*, org.hibernate.proxy, javassist.util.proxy</Dynamic-Import-Package>

Step 2 : Adapt the persistence file

We will cover here how to modify a persistence.xml for OSGi usage. For the most part only the access to the DataSource has to be adapted for OSGi. With J2EE applications, you simply use the jdbc key with the name of the datasource associated (jdbc/reportincidentdb). In OSGi jndi support is provided by aries jndi ( It bridges jndi names to OSGi services. We must define two parameters, the "osgi:service" wich will allow to lookup OSGI services, the interface "javax.sql.DataSource" and the name of the service "", which is a filter property, with its jndi name associated.

To access to the datasource, you must provide within the or depending if you use transaction type JTA or RESOURCE_LOCAL.

<persistence-unit name="tasklist" transaction-type="JTA">

The other elements of the xml file are defined according to JPA specification.

Step 3.1 : Inject EntityManager into a bean and make it transactional

The goal of this step is to provide a DAO layer that looks like JEE code on the java level. For this we need to inject a thread safe EntityManager and ensure the DAO code is run inside a transational context.

Aries JPA 1.x used a xml element inside each DAO bean to inject the EntityManager. This syntax is not suppoerted for Aries JPA 2.x anymore. Instead simply enable standard @PesistenceContext and @PersistenceUnit annotation support with the xml element on top level.

The transactional context is established using the xml element on the bean level. In the example below we enable transactions for all DAO methods. The scope of the transaction can be defined using the attribute value.

Example blueprint follows showing the full breadth of allowable injection syntax:

<blueprint xmlns="" 
  <jpa:enable />
  <service ref="taskService" interface="org.apache.aries.jpa.example.tasklist.model.TaskService"/>
  <bean class="org.apache.aries.jpa.example.tasklist.blueprint.impl.TaskServiceImpl"/>
    <tx:transaction method="*"/>

Make sure you inject the EntityManager in your DAO class like this:

EntityManager em;

See tasklist-blueprint example for details.

Step 3.2 : Use JPATemplate to work with JPA in declarative services

Inject the JPATemplate using a service reference:

@Reference(target = "(")
public void setJpaTemplate(JpaTemplate jpa) { ... }

Use the JPATemplate to work with JPA Entities inside closures.

// txExpr if you need to return an object
return jpa.txExpr(TransactionType.Required, em -> em.find(Task.class, id));

// tx if you just execute code
jpa.tx(em -> em.persist(task));

See the tasklist-ds example for details.

Step 4 : Package the solution

To package the solution, execute a "maven clean install" instruction. Installing Aries JPA and Aries Transaction into arbitrary containers is beyond the scope of this document.

To keep the installation instructions small we only cover installation into Apache Karaf 4.x. Karaf provides features for Aries JPA, Aries Transaction, Hibernate and Pax-jdbc so installation is very easy. See the README at