EJB3 JPA
OpenXava supports EJB3 since the version 2.1.
Configure your application to work with EJB3
Since v3.0 JPA is the default persistence engine, so you have to do nothing. But if you are using a v2.1 or v2.2 and you want to use JPA then edit the file
properties/xava.properties of your project, and left it in this way:
# EJB3 Java Persistence API
persistenceProviderClass=org.openxava.model.impl.JPAPersistenceProvider
mapFacadeAsEJB=false
From now on, your application will use EJB3 JPA instead of Hibernate to manage object persistence.
Using EJB3 JPA in your code
You can use the standard
Java Persistence API in any part of an OpenXava application, that is, inside calculators, validators, actions, filters, etc.
In order to facilitate the use of JPA OpenXava provides the
XPersistence class. For example, if you wish to store an object in a database using the JPA, the normal way would be:
EntityManagerFactory f = Persistence.createEntityManagerFactory("default");
EntityManager manager = f.createEntityManager();
manager.getTransaction().begin();
Customer customer = ... ;
manager.persist(customer);
manager.getTransaction().commit();
manager.close();
But, inside OpenXava and using
XPersistence class you can write this:
Customer customer = ... ;
XPersistence.getManager().persist(customer);
No more.
The first time that you call to
XPersistence.getManager() a manager is created and assigned to the current thread and a transaction is created too; the next time that you call it, the same manager is used. At the end of the complete cycle of action execution, OpenXava commits automatically the transaction and closes the manager.
You can optionally commit the transaction in any moment calling to
XPersistence.commit(), if after this you use
XPersistence.getManager() an new manager and a new transaction are created for you.
You can learn more seeing the API doc of
org.openxava.jpa.XPersistence class.