You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Added in 5.3

Starting from release 5.3, Tapestry provides a built-in JPA integration which supersedes Tynamo's JPA integration.

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.

<?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.username"
                    value="sa" />
          <property name="eclipselink.ddl-generation"
                    value="create-tables"/>
          <property name="eclipselink.logging.level"
                    value="fine"/>
      </properties>
   </persistence-unit>

</persistence>

By default, the persistence descriptor is named persistence.xml and is expected to be located on the classpath in the META-INF directory. If you want to place the persistence.xml file in an other directory or name it arbitrarily, you can make a contribution to the SymbolProvider service, as shown in the following example. This is a quite useful feature if you want to use a different persistence descriptor for tests.

public class AppModule {

    @Contribute(SymbolProvider.class)
    @FactoryDefaults
    public static void provideFactoryDefaults(final MappedConfiguration<String, String> configuration) {
        configuration.add(JpaSymbols.PERSISTENCE_DESCRIPTOR, "/org/example/persistence.xml");
    }

}

XML-less JPA configuration

Configuring JPA with Tapestry is much more simple than defined in the JPA specification. Tapestry allows you to configure the EntityManagerFactory programmatically without writing XML configuration files. Imagine you want to use JDBC connections managed by the container and provided through JNDI. The resulting persistence descriptor might look like the following one.

<?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
      </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 to write any single line of XML. The following example demonstrates an equivalent JPA configuration.

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);
   }
}
  • No labels