Kontakt

XML-less JPA configuration

Posted on Donnerstag, 26th Mai, 2011

In the last post I gave you a short preview of the Tapestry/JPA integration library in the upcoming 5.3 release. In this post I’m going to show a new feature that I added yesterday. You will see that configuring JPA with Tapestry is much more simple than defined in the JPA specification.

The persistence.xml file is the standard configuration file in JPA used to define the persistence units. Let’s explore the following persistence descriptor using a non-JTA data source.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             version="2.0">
   <persistence-unit name="Blue"
                     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 an equivalent JPA configuration without using any XML descriptors. This can be accomplished by making a contribution to the EntityManagerSource service.

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("Blue", configurer);
   }
}

The EntityManagerSource service’s configuration is a map in which the keys are persistence unit names and the values are PersistenceUnitConfigurer instances. In other words, a PersistenceUnitConfigurer instance is associated with a persistence unit to be configured. The TapestryPersistenceUnitInfo instance passed to the PersistenceUnitConfigurer holds the persistence unit metadata for use by the persistence provider. It may represent a persistence unit defined in the persistence.xml file or an empty persistence unit. Confused? Let me clarify it.

If the contribution key matches a persistence unit defined in the persistence.xml file, then the passed TapestryPersistenceUnitInfo instance is pre-filled with the metadata from persistence.xml. This metadata can be modified programmatically inside a PersistenceUnitConfigurer. If persistence.xml file is not present or the contribution key doesn’t match any persistence unit, the passed TapestryPersistenceUnitInfo instance is empty. This way Tapestry allows you to configure JPA without writing XML descriptor files.

Enjoy!

Kommentare sind geschlossen.

Tapestry 5 Blog - Copyright © 2009 - Eclectic Theme by Your Inspiration Web - Powered by WordPress