public class JpaBuilder extends Object implements TransactionResource<javax.persistence.EntityManager>
Modifier and Type | Class and Description |
---|---|
static interface |
JpaBuilder.JpaUnitOfWork<T>
Callback interface to execute some JPA code within a transaction with the
entitymanager to use provided as input.
|
Constructor and Description |
---|
JpaBuilder(String aUrl,
String aUser,
String aPassword,
PersistenceUnitDescription aPersistenceUnit)
Constructs the builder.
|
Modifier and Type | Method and Description |
---|---|
javax.persistence.EntityManager |
begin()
Begins a transaction.
|
TransactionResult |
commit(javax.persistence.EntityManager aEntityManager)
Commits the transaction.
|
javax.persistence.EntityManagerFactory |
createFactory()
Creates a new entity manager factory.
|
<T> T |
execute(JpaBuilder.JpaUnitOfWork<T> aWork)
Executes a unit of work.
|
<T> T |
execute(JpaBuilder.JpaUnitOfWork<T> aWork,
TransactionResultCallback aCallback)
Executes a unit of work.
|
javax.persistence.EntityManager |
getContextualEntityManager()
Gets a contextual reference to an entity manager that delegates to the
appropriate (current) one which is active for the current transaction.
|
TransactionResult |
rollback(javax.persistence.EntityManager aEntityManager)
Rolls back a transaction.
|
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.
|
public JpaBuilder(String aUrl, String aUser, String aPassword, PersistenceUnitDescription aPersistenceUnit)
aUrl
- JDBC URLaUser
- User nameaPassword
- Password.aPersistenceUnit
- Persistence unit.public void start() throws Exception
Exception
public void stop()
public javax.persistence.EntityManagerFactory createFactory()
public <T> T execute(JpaBuilder.JpaUnitOfWork<T> aWork) throws Exception
JpaBuilder.JpaUnitOfWork.execute(EntityManager)
within a transaction,
passing it the entity manager. Use of this method saves a lot of typing
for applications.
This method requires the transaction to succeed. Otherwise the test will
fail. See execute(JpaUnitOfWork, TransactionResultCallback)
and
RequireTransactionStatus
for more possibilities.aWork
- Work to execute.Exception
public <T> T execute(JpaBuilder.JpaUnitOfWork<T> aWork, TransactionResultCallback aCallback) throws Exception
JpaBuilder.JpaUnitOfWork.execute(EntityManager)
within a transaction,
passing it the entity manager. Use of this method saves a lot of typing
for applications.aWork
- Work to execute.aTransactionResultCallback
- callback to notify of the result of the transaction.Exception
public javax.persistence.EntityManager begin()
TransactionResource
begin
in interface TransactionResource<javax.persistence.EntityManager>
public TransactionResult commit(javax.persistence.EntityManager aEntityManager)
TransactionResource
commit
in interface TransactionResource<javax.persistence.EntityManager>
aEntityManager
- Object that manages the transaction for the resource.public TransactionResult rollback(javax.persistence.EntityManager aEntityManager)
TransactionResource
rollback
in interface TransactionResource<javax.persistence.EntityManager>
aEntityManager
- Object that manages the transaction for the resource.public javax.persistence.EntityManager getContextualEntityManager()
Copyright © 2022. All rights reserved.