Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed typo.


Since
since5.3
 

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

...

Code Block
languagexml
titlepom.xml (partial) for Hibernate
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>${hibernate-version}</version>
    <exclusions>
        <exclusion>
            <!-- omit Geronimo JPA spec to avoid conflict with Hibernate JPA spec -->
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-jpa_2.0_spec</artifactId>
        </exclusion>
    </exclusions>
</dependency>

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.

Code Block
languagexml
langxml
titlepersistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
   <persistence-unit name="DemoUnit" transaction-type="RESOURCE_LOCAL">
       <properties>
          <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
          <property name="javax.persistence.jdbc.url"    value="jdbc:h2:mem:test" />
          <property name="javax.persistence.jdbc.user"   value="sa" />
          <property name="eclipselink.ddl-generation"    value="create-tables"/>
          <property name="eclipselink.logging.level"     value="fine"/>
      </properties>
   </persistence-unit>

</persistence>

...

With Tapestry, configuring JPA is can be much simpler than as described in by the JPA specification. Tapestry allows you to configure the EntityManagerFactory programmatically, without writing any XML. Imagine For example, imagine that you want to use JDBC connections managed by the container and provided through JNDI. The resulting persistence descriptor might look like this:

Code Block
languagexml
langxml
titlepersistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             version="2.0">
   <persistence-unit name="JTAUnit" transaction-type="RESOURCE_LOCAL">

      <non-jta-data-source>
         jdbc/JPATest
      <source>jdbc/JPATest</non-jta-data-source>

      <properties>
         <property name="eclipselink.ddl-generation" value="create-tables"/>
         <property name="eclipselink.logging.level" value="fine"/>
      </properties>
   </persistence-unit>

</persistence>

Now let's see how to provide the same configuration without XML. The following demonstrates an equivalent JPA configuration.

Code Block
languagejava
titleAppModule.java (partial)
public class AppModule {

   @Contribute(EntityManagerSource.class)
   public static void configurePersistenceUnitInfos(MappedConfiguration<String,PersistenceUnitConfigurer> cfg) {

      PersistenceUnitConfigurer configurer = new PersistenceUnitConfigurer() {

         public void configure(TapestryPersistenceUnitInfo unitInfo) {

            unitInfo.nonJtaDataSource("jdbc/JPATest")
               .addProperty("eclipselink.ddl-generation", "create-tables")
               .addProperty("eclipselink.logging.level", "fine");
         }
     };

     cfg.add("JTAUnit", configurer);
   }
}

...

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.

Injecting the EntityManager into page and component classes

Configuration Settings

Several aspects of Tapestry-jpa can be customized in your application module (usually AppModule.java), just like other Tapestry configuration symbols.

SymbolDefaultDescription
JpaSymbols.PROVIDE_ENTITY_VALUE_ENCODERStrueWhether entity value encoders will be provided automatically. See Using Select with a List.
JpaSymbols.EARLY_START_UPtrueWhether JPA will be started up at application launch, rather than lazily.
JpaSymbols.ENTITY_SESSION_STATE_PERSISTENCE_STRATEGY_ENABLEDtrue

Whether the "entity" persistence strategy is used to store JPA entities as Session State Objects.

JpaSymbols.PERSISTENCE_DESCRIPTOR/META-INF/persistence.xmlThe 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 EntityManager into page and component classes

Depending on whether Depending on whether more than one persistence unit has been defined, the way to inject EntityManager 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 @PersistenceContext annotation.

...

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
languagejava
titleUserDaoImpl
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.

...