...
Starting from release 5.3, Tapestry provides a built-in JPA integration which supersedes Tynamo's JPA integration.
Contents
Table of Contents | ||||
---|---|---|---|---|
|
Configuring JPA
The persistence.xml file is the standard configuration file in JPA used to define the persistence units. Tapestry reads this file to create the EntityManagerFactory. The following example demonstrates a persistence.xml file.
...
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.
Injecting the EntityManager into page and component classes
Depending on whether more than one persistence unit has been defined, the way to inject [EntityManager|http://download.oracle.com/javaee/6/api/javax/persistence/EntityManager.html] varies slightly. Let’s start with a simple scenario, where only a single persistence unit is defined. In this case, an EntityManager can be injected using the @Inject annotation.
Code Block |
---|
public class CreateAddress {
@Inject
private EntityManager entityManager;
@Property
private Address address;
@CommitAfter
void onSuccess() {
entityManager.persist(address);
}
} |
Alternatively, you can use the @PersistenceContext annotation to get the EntityManager_ _injected into a page or component, as shown in the following example.
Code Block |
---|
public class CreateAddress {
@PersistenceContext
private EntityManager entityManager;
@Property
private Address address;
@CommitAfter
void onSuccess() {
entityManager.persist(address);
}
} |
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 CreateAddress {
@PersistenceContext(unitName = "DemoUnit")
private EntityManager entityManager;
@Property
private Address address;
@CommitAfter
@PersistenceContext(unitName = "DemoUnit")
void onSuccess() {
entityManager.persist(address);
}
} |
Injecting EntityManager into services
While component injection occurs only on fields, the injection in the IoC layer may be triggered by a field or a constructor. The following example demonstrates field injection, when a single persistence unit is defined in the persistence descriptor.
Code Block |
---|
public class UserDAOImpl implements UserDAO {
@Inject
private EntityManager entityManager;
...
} |
The constructor injection is demonstrated in the following example.
Code Block |
---|
public class UserDAOImpl implements UserDAO {
private EntityManager entityManager;
public UserDAOImpl(EntityManager entityManager) {
this.entityManager = entityManager;
}
...
} |
If multiple persistence units are defined in the same application, you need to disambiguate the unit to inject. This is done by placing the @PersistenceContext annotation, as shown in the following example. Because @PersistenceContext annotation must not be placed on constructor parameters, you can’t use constructor injection and must switch to field injection.
Code Block |
---|
public class UserDAOImpl implements UserDAO { @Inject @PersistenceContext(unitName = "DemoUnit") private EntityManager entityManager; ... } |