Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

...

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.

...

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

In the example above you can see a contribution to the EntityManagerSource service. This service is responsible for creating EntityManagerFactory to be used to create EntityManager. When the service is initialized, it parses the persistence.xml, file if available. For any persistence unit defined in the XML descriptor an TapestryPersistenceUnitInfo is created. The TapestryPersistenceUnitInfo interface is a mutable extension of the PersistenceUnitInfo interface (defined in the JPA specification) that allows you to configure a persistence unit programmatically. After parsing persistence descriptor EntityManagerSource service applies its configuration to create further persistence units and/or update the existing.

The EntityManagerSource service’s configuration is a map in which persistence unit names are associated with PersistenceUnitConfigurer instances. A PersistenceUnitConfigurer is used to configure a persistence unit programmatically that has been associated with it. In the example above you can see a contribution providing a PersistenceUnitConfigurer for the unit named JTAUnit.

What happens if you contribute a PersistenceUnitConfigurer for a persistence unit that has been defined in the persistence.xml file?