Tapestry JPA provides integration between Tapestry 5 and the JPA Specifications ( JPA1: JSR-220 and JPA2: JSR-317). It is modeled after tapestry's hibernate module and provides the same features. It has been used in production for some time now and is considered to be stable.
In order to start using Tapestry JPA, you need to meet the following requirements.
- working persistence.xml with defined PersistenceUnit as you would have in any other JPA project
- a implementation of JPA2 in your classpath. We use EclipseLink 2.0.0 in our examples, but any other provider should work as well.
- a tapestry-ioc module for tapestry-jpa-core or a tapestry webapp for the extended features of tapestry-jpa
You need the following modules in order to use the JPA functionality.
- tapestry-jpa (for use with tapestry web applications)
- tapestry-jpa-core (for use with tapestry-ioc modules)
These artifacts are available in the maven main repository. No need to configure any additional repositories.
For the module to work, you first need to tell tapestry-jpa what PersistenceUnit to use.
That should be everything needed.
How to use Tapestry JPA
Injecting the EntityManager
In any Service, Page or Component you may inject an
EntityManager like in the following example:
Committing with tapestry-jpa
You may manage your transactions manually by retrieving a transaction using
em.getTransaction(). But to make your life easier, you can use the
Annotating a method with
@CommitAfter will automatically commit the transaction after successfull execution. It is rollbacked if an exception is thrown.
Committing with tapestry-jpa-core
If you are using tapestry-jpa-core in a tapestry-ioc module, the @CommitAfter does not work. To make it work, you need to do something like this:
@Match annotation which says what interfaces should be checked for the
@CommitAfter annotation. Other useful values could be "*Service". In this case all interfaces named
CustomerService would be matched.
Because tapestry-ioc is not able to check the annotations inside the implementation, it must be in the interface!