de.akquinet.jbosscc.needle.db.transaction
Class TransactionHelper

java.lang.Object
  extended by de.akquinet.jbosscc.needle.db.transaction.TransactionHelper

public class TransactionHelper
extends Object

Utility class to manage transactions conveniently.


Constructor Summary
TransactionHelper(javax.persistence.EntityManager manager)
           
 
Method Summary
<T> T
executeInTransaction(Runnable<T> runnable)
          see executeInTransaction(runnable, clearAfterCommit) .
<T> T
executeInTransaction(Runnable<T> runnable, boolean clearAfterCommit)
          Encapsulates execution of runnable.run() in transactions.
 javax.persistence.EntityManager getEntityManager()
           
<T> List<T>
loadAllObjects(Class<T> clazz)
          Returns all objects of the given class in persistence context.
<T> T
loadObject(Class<T> clazz, Object id)
          Finds and returns the object of the given id in the persistence context.
<T> T
loadObject(javax.persistence.EntityManager entityManager, Class<T> clazz, Object id)
           
<T> T
persist(T obj)
           
<T> T
persist(T obj, javax.persistence.EntityManager entityManager)
           
<T> T
saveObject(T obj)
          Saves the given object in the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransactionHelper

public TransactionHelper(javax.persistence.EntityManager manager)
Method Detail

saveObject

public final <T> T saveObject(T obj)
                   throws Exception
Saves the given object in the database.

Type Parameters:
T - type of given object obj
Parameters:
obj - object to save
Returns:
saved object
Throws:
Exception - save objects failed

loadObject

public final <T> T loadObject(Class<T> clazz,
                              Object id)
                   throws Exception
Finds and returns the object of the given id in the persistence context.

Type Parameters:
T - type of searched object
Parameters:
clazz - type of searched object
id - technical id of searched object
Returns:
found object
Throws:
Exception - finding object failed

loadAllObjects

public final <T> List<T> loadAllObjects(Class<T> clazz)
                             throws Exception
Returns all objects of the given class in persistence context.

Type Parameters:
T - type of searched objects
Parameters:
clazz - type of searched objects
Returns:
list of found objects
Throws:
Exception - finding objects failed

executeInTransaction

public final <T> T executeInTransaction(Runnable<T> runnable,
                                        boolean clearAfterCommit)
                             throws Exception
Encapsulates execution of runnable.run() in transactions.

Type Parameters:
T - result type of runnable.run()
Parameters:
runnable - algorithm to execute
clearAfterCommit - true triggers entityManager.clear() after transaction commit
Returns:
return value of runnable.run()
Throws:
Exception - execution failed

executeInTransaction

public final <T> T executeInTransaction(Runnable<T> runnable)
                             throws Exception
see executeInTransaction(runnable, clearAfterCommit) .

Type Parameters:
T - result type of runnable.run()
Parameters:
runnable - algorithm to execute
Returns:
return value of runnable.run()
Throws:
Exception - execution failed

getEntityManager

public final javax.persistence.EntityManager getEntityManager()

persist

public <T> T persist(T obj,
                     javax.persistence.EntityManager entityManager)

persist

public <T> T persist(T obj)

loadObject

public <T> T loadObject(javax.persistence.EntityManager entityManager,
                        Class<T> clazz,
                        Object id)


Copyright © 2013 akquinet AG. All Rights Reserved.