Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Minor formatting

...

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);
   }
}

...

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
languagejava
titleCreateAddress.java
public class CreateAddress {

   @Inject
   private EntityManager entityManager;

   @Property
   private Address address;

   @CommitAfter
   void onSuccess() {
      entityManager.persist(address);
   }
}

...