Deploy a JPA application to the Liberty profile
To enable the Liberty profile to support an application that uses the Java Persistence API (JPA), we add the jpa-2.0 feature to server.xml. You also need to define persistence contexts and persistence units, and configure access to the entity manager and entity manager factory.
This task assumes that we have created a Liberty profile server, on which to deploy an application that uses JPA. See Create a Liberty profile server manually.
The jpa-2.0 feature provides support for applications that use application-managed and container-managed JPA written to the JPA 2.0 specification. The support is built on Apache OpenJPA with extensions to support the container-managed programming model.
- Add the jpa-2.0 feature to server.xml.
- Add persistence context and persistence unit definitions to the web.xml file.
For example:
<persistence-context-ref> <persistence-context-ref-name>example/em</persistence-context-ref-name> <persistence-unit-name>ExamplePersistenceUnit</persistence-unit-name> </persistence-context-ref> <persistence-unit-ref> <persistence-unit-ref-name>example/emf</persistence-unit-ref-name> <persistence-unit-name>ExamplePersistenceUnit</persistence-unit-name> </persistence-unit-ref>
- Configure access to the entity manager.
For example:
Context ctx = new InitialContext(); UserTransaction tran = (UserTransaction) ctx.lookup("java:comp/UserTransaction"); tran.begin(); EntityManager em = (EntityManager) ctx.lookup(java:comp/env/example/em"); Thing thing = new Thing(); em.persist(thing); tran.commit();
- Configure access to the entity manager factory.
For example:
Context ctx = new InitialContext(); EntityManagerFactory emf = (EntityManager) ctx.lookup(java:comp/env/example/emf"); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Thing thing = new Thing(); em.persist(thing); tx.commit(); int id = thing.getId(); em.close();
Subtopics
- Enhancement of JPA entities
The JPA provider included in the Liberty profile is based on Apache OpenJPA. OpenJPA uses Java bytecode enhancement of JPA persistent types (Entity, Embeddable, MappedSuperclass) to add state tracking, and other necessary information to enable persistence and other optimized features within JPA classes. In an application server environment, enhancement of the JPA entities occurs automatically when the application is loaded by the Liberty profile server.
Parent topic: Deploy applications to the Liberty profileReference:
Apache OpenJPA javadocLiberty features