org.wamblee.support.persistence
Class JpaBuilder

java.lang.Object
  extended by org.wamblee.support.persistence.JpaBuilder

public class JpaBuilder
extends java.lang.Object

Utility for building an appropriately configured EntityManagerFactory. The idea is that a persistence.xml is used unchanged from the production version. This utility will then add the additional properties required for execution in a standalone environment. The other purpose is to to shield dependencies of the test code on a particular JPA provider.


Nested Class Summary
static interface JpaBuilder.JpaUnitOfWork<T>
          Callback interface to execute some JPA code within a transaction with the entitymanager to use provided as input.
 
Constructor Summary
JpaBuilder(javax.sql.DataSource aDataSource, PersistenceUnitDescription aPersistenceUnit)
          Constructs the builder.
 
Method Summary
 javax.persistence.EntityManagerFactory createFactory()
          Creates a new entity manager factory.
<T> T
execute(JpaBuilder.JpaUnitOfWork<T> aWork)
          Executes a unit of work.
 void start()
          Starts the builder, which in particular, mocks JNDI, binds the datasource the JNDI where the persistence unit expects it, creates the entity manager factory, and forces creation of the database schema.
 void stop()
          Stops the entity manager factory and disables JNDI mocking.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JpaBuilder

public JpaBuilder(javax.sql.DataSource aDataSource,
                  PersistenceUnitDescription aPersistenceUnit)
Constructs the builder.

Parameters:
aDataSource - Datasource of database.
aPersistenceUnit - Persistence unit.
Method Detail

start

public void start()
           throws java.lang.Exception
Starts the builder, which in particular, mocks JNDI, binds the datasource the JNDI where the persistence unit expects it, creates the entity manager factory, and forces creation of the database schema.

Throws:
java.lang.Exception

stop

public void stop()
Stops the entity manager factory and disables JNDI mocking.


createFactory

public javax.persistence.EntityManagerFactory createFactory()
Creates a new entity manager factory. Typically not used by test code.

Returns:
Entity manager factory.

execute

public <T> T execute(JpaBuilder.JpaUnitOfWork<T> aWork)
          throws java.lang.Exception
Executes a unit of work. This creates an entitymanager and runs the JpaBuilder.JpaUnitOfWork.execute(EntityManager) within a transaction, passing it the entity manager. Use of this method saves a lot of typing for applications.

Parameters:
aWork - Work to execute.
Returns:
The return value of the execute method of the unit of work.
Throws:
java.lang.Exception


Copyright © 2010. All Rights Reserved.