Since | ||
---|---|---|
| ||
Starting with Tapestry 5.3, Tapestry provides a built-in integration with the Java Persistence API (JPA) through the Tapestry-jpa module. This module supersedes the 3rd-party Tynamo JPA module.
Contents
...
Info |
---|
Note that the TapestryPersistenceUnitInfo instance passed to the PersistenceUnitConfigurer is either empty or my may contain the persistence unit metadata read from the persistence.xml file. What happens if you contribute a PersistenceUnitConfigurer for a persistence unit that has not been defined in the persistence.xml file? In this case Tapestry assumes that you want to configure the persistence unit programmatically and just creates a fresh TapestryPersistenceUnitInfo object and passes it to the PersistenceUnitConfigurer. |
...
As you can see, you may add as many packages as you wish.
Injecting the EntityManager
The created entity managers can be injected into page, component and other services.
Configuration Settings
Several aspects of Tapestry-jpa can be customized in your application module (usually AppModule.java), just like other Tapestry configuration symbols.
Symbol | Default | Description |
---|---|---|
JpaSymbols.PROVIDE_ENTITY_VALUE_ENCODERS | true | Whether entity value encoders will be provided automatically. See Using Select with a List. |
JpaSymbols.EARLY_START_UP | true | Whether JPA will be started up at application launch, rather than lazily. |
JpaSymbols.ENTITY_SESSION_STATE_PERSISTENCE_STRATEGY_ENABLED | true | Whether the "entity" persistence strategy is used to store JPA entities as |
JpaSymbols.PERSISTENCE_DESCRIPTOR | /META-INF/persistence.xml | The location of the persistence configuration file, located on the classpath |
Injecting the EntityManager
The created entity managers can be injected into page, component and other services.
Injecting the Injecting the EntityManager into page and component classes
...
Alternatively, you can use the @Inject annotation to get the EntityManager injected into a page or component, as shown in the following example.
Code Block | ||||
---|---|---|---|---|
| ||||
public class CreateAddress { @Inject private EntityManager entityManager; @Property private Address address; @CommitAfter void onSuccess() { entityManager.persist(address); } } |
However, if you have multiple instances of persistence-unit defined in the same application, you need to explicitly tell Tapestry which persistence unit you want to get injected. This is what the @PersistenceContext annotation’s name attribute is used for? . The following example demonstrates how to inject the persistence unit named DemoUnit.
...
Code Block | ||||
---|---|---|---|---|
| ||||
public class UserDaoImpl implements UserDao {
@Inject
@PersistenceContext(unitName = "DemoUnit")
private EntityManager entityManager;
...
} |
...
"DemoUnit")
private EntityManager entityManager;
...
} |
Value Encoders
The Tapestry-jpa module automatically provides value encoders to make it easy to work with entities (especially lists of entities) in your Tapestry pages and components. This is modeled on the similar functionality from the Tapestry-hibernate-core module. See the Hibernate User Guide for all the details.
Transaction Management
As you may already know from the Hibernate integration library, Tapestry automatically manages transactions for you. The JPA integration library defines the @CommitAfter annotation, which acts as the correspondent annotation from the Hibernate integration library. Let’s explore the UserDao interface to see the annotation in action.
...