Developing with Java Persistence API in eXo Platform

eXo Platform 4.3 comes with an integration of JPA (Java Persistence API). JPA is a Java application programming interface specification that describes the management of relational data in Java applications. It is used to define how a Java class (called an entity) maps to a relational database table through standard annotations or XML. It also defines an EntityManager API for processing queries and transactions on the Java objects against the database.


If you are not familiar with JPA, you can learn more in the official documentation.

The integration we did in eXo Platform allowed us to start the move to our new data model and gave our developers the opportunity to build their own applications easily with JPA.
This article describes this integration and explains how to develop applications using JPA in eXo Platform.

Hibernate JPA integration

We use Hibernate as the JPA provider, since it is the reference implementation. Starting with eXo Platform 4.3, it is easy to map Java entities with JPA/Hibernate. We have prepared everything for you: the data source, persistence unit, entity annotations and generic DAO.

Data source

A new data source is available in eXo Platform by default, called “exo-jpa_portal” (you can change this name thanks to the property “” in the file). This data source is used for all operations that use the eXo persistence unit.

Persistence unit and entity manager

All JPA operations need a persistence unit and an entity manager to act on a database.

The persistence unit defines the data source to use, the entities to manage and a set of configuration parameters.
We added a default persistence unit in eXo Platform, called exo-pu, which uses the default data source exo-jpa_portal, and does not declare any entities:

The configuration of this persistence unit can be changed easily by setting parameters, prefixed by “exo.jpa.”, in the file. For example, you can tell JPA Hibernate to log all SQL queries by adding the following line in


eXo Platform provides an annotation to add a JPA entity to its persistence unit (exo-pu): @ExoEntity. Let’s see an example:

This is an extract of the task entity. As you can see, it is a standard JPA entity (annotated by @Entity). The only difference is the addition of the annotation @ExoEntity, nothing else. Adding this annotation and putting this class in the server classpath automatically adds this entity to the eXo persistence unit.


@ExoEntity is not the only annotation provided by eXo Platform; we also added an annotation to define transactional methods: @ExoTransactional.
Setting this annotation on a method automatically uses the eXo entity manager to start a new transaction and close it, or to use an existing transaction if one is already open.

To make it work, the JCabi Maven plugin must be enabled in your Maven configuration. You have two options:

  • Either use the eXo addon parent POM (Project Object Model) as your project parent POM, as Task does:

and declare the plugin JCabi in the module that uses @ExoTransactional:

  • Or add the JCabi Maven plugin with the right configuration:

Generic DAO

To make developers’ lives even easier, we added a generic DAO, which defines all the basic operations, using the eXo entity manager: find, findAll, create, update, delete, …

You just need to extend your DAO class from the class org.exoplatform.commons.persistence.impl.GenericDAOJPAImpl to benefit from all these methods:

Initialize and update your data model

Now that we have designed our model in a Java object, we need to create the data.

Hibernate allows us to generate the data schema automatically from the Java model, thanks to the JPA annotations and the configuration property

We believe that the modelization of the database is a very important phase in designing a solution. It should not rely exclusively on automatic generation from Hibernate but rather it should be part of a dedicated process (design and generation) managed by developers. It is important for the developers to master the model and fully understand it.

eXo Platform comes with Liquibase, a tool that allows you to init and incrementally update a database schema and the data. Its use is not mandatory for building custom applications, of course. It simply provides a good and efficient way to manage a database. It’s what we use for eXo applications.

Liquibase relies on changelog files to init and update the database. These files define a bunch of change sets. Each change set defines an operation on the database (table creation, constraint creation, index creation, SQL query execution, …). As an example, here is an extract of the change logs of the eXo Task application:

The full content of the change log file of Task 1.0 is available at

As soon as a change set is applied, Liquibase remembers it and does not apply it again on subsequent calls. It relies on the change set ID, so it is very important not to update it once applied, and of course don’t have two identical IDs.

eXo Platform launches Liquibase at each startup. For the Task application, for example, this means that this change log file will be executed every time you restart your eXo Platform instance. If the Task data have never been initialized, all these change sets will be executed. Then, on subsequent calls, nothing will be done, until we add some new change sets.

To let eXo Platform know which change log files to apply, you simply have to add the following configuration in your extension:

The value of the changelogs parameter allows you to define a list of paths (in the classpath) for the change log files. In this example, we define the change logs of Task 1.0.0 and 1.1.0. The convention we use at eXo is one change log file per version, but it is up to you to organize your change logs as you want.

Once these files are in your extension, you are ready to generate your first data schema using Liquibase in eXo Platform.

You can learn more about Liquibase from its official documentation.


Now that you know how to declare JPA entities, configure the persistence unit, create DAO classes, manage transactions and init/update your data, you are all set to develop applications with JPA.

eXo Platform takes care of all the boilerplate code, so you can focus on the essentials.

Of course, you can still manage all of this by yourself (creating and updating your data schema, creating your persistence unit, declaring your entities, implementing default DAO methods, managing transactions, …), but is it really worthwhile? 😉

Related Posts
Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">