@Status(stage=TEST, unitTests=PARTIAL) @Review(by="Kees Schotanus", at="2009-09-28") public final class EntityManagerHelper extends Object
EntityManager that
don't directly deal with a JPA Entity.EntityManagerFactory instance.| Modifier and Type | Method and Description |
|---|---|
static void |
beginTransaction()
Begins a transaction.
|
static void |
clear()
Clear the persistence context.
|
static void |
closeEntityManager()
Closes the EntityManager.
|
static void |
commit()
Commits the transaction and clears the cache.
|
static javax.persistence.Query |
createNamedQuery(String name)
Creates a named Query.
|
static <T> javax.persistence.TypedQuery<T> |
createNamedQuery(String name,
Class<T> type)
Creates a named Query.
|
static javax.persistence.Query |
createNativeQuery(String query)
Creates a native Query.
|
static javax.persistence.Query |
createNativeQuery(String query,
Class<? extends Persistable> type)
Creates a native Query with resulting object of the supplied type.
|
static javax.persistence.Query |
createQuery(String query)
Creates a Query.
|
static void |
detach(Persistable persistable)
Detaches the supplied persistable by removing it from the persistence
context.
|
static void |
flush()
Synchronizes the persistence context to the underlying database.
|
static javax.persistence.EntityManager |
getEntityManager()
Gets the EntityManager for this Thread.
|
static javax.persistence.FlushModeType |
getFlushMode()
Gets the flush mode, applicable to all Entities in this persistence
context.
|
static javax.persistence.EntityTransaction |
getTransaction()
Gets the resource-level transaction object.
|
static void |
init(String persistenceUnitName)
Initialize this EntityManagerHelper with the supplied
persistenceUnitName.
You only need to call this method when you want to use another persistence unit than the default persistence unit. |
static void |
isRollbackOnly()
Determines whether the current transaction has been marked for
'rollback only'.
|
static void |
joinTransaction()
Joins an active transaction.
This method should be called on a JTA application managed EntityManager that was created outside the scope of the active transaction. |
static void |
rollback()
Rolls back the transaction, provided a transaction is active.
|
static void |
setFlushMode(javax.persistence.FlushModeType flushMode)
Sets the flush mode, applicable to all Entities in this persistence
context.
|
static void |
setRollbackOnly()
Marks the current transaction for 'rollback only'.
|
public static void init(String persistenceUnitName)
persistenceUnitName - The name of the persistence unit.
public static javax.persistence.EntityManager getEntityManager()
public static void closeEntityManager()
IllegalStateException - When the EntityManager is container-managed
or has already been closed.public static javax.persistence.EntityTransaction getTransaction()
IllegalStateException - When invoked on a JTA EntityManager.EntityManager.getTransaction()public static void beginTransaction()
IllegalStateException - When a transaction is already active.EntityTransaction.begin()public static void commit()
IllegalStateException - When the transaction is not active.javax.persistence.RollbackException - When the commit fails.EntityTransaction.commit()public static void rollback()
IllegalStateException - When invoked on a JTA EntityManager.javax.persistence.PersistenceException - When an unexpected error
condition is encountered.EntityTransaction.rollback()public static void setRollbackOnly()
IllegalStateException - When the transaction is not active.EntityTransaction.setRollbackOnly()public static void isRollbackOnly()
IllegalStateException - When the transaction is not active.EntityTransaction.getRollbackOnly()public static void joinTransaction()
IllegalStateException - When the EntityManager is closed.javax.persistence.TransactionRequiredException - When there is no
active transaction.EntityManager.joinTransaction()public static javax.persistence.Query createQuery(String query)
query - The SQL query.IllegalArgumentException - When the supplied query is not valid.IllegalStateException - When the EntityManager is closed.NullPointerException - When the supplied query is null.EntityManager.createQuery(String)public static javax.persistence.Query createNamedQuery(String name)
name - The name of the Query.EntityManager.createNamedQuery(String)public static <T> javax.persistence.TypedQuery<T> createNamedQuery(String name, Class<T> type)
T - The type returned by the named query.name - The name of the Query.type - The type returned by the query.EntityManager.createNamedQuery(String, Class)public static javax.persistence.Query createNativeQuery(String query)
query - The native SQL query.IllegalArgumentException - When the supplied query is not valid.IllegalStateException - When the EntityManager is closed.NullPointerException - When the supplied query is null.EntityManager.createNativeQuery(String)public static javax.persistence.Query createNativeQuery(String query, Class<? extends Persistable> type)
query - The native SQL query.type - Type of the returned objects.IllegalArgumentException - When the supplied query is not valid.IllegalStateException - When the EntityManager is closed.NullPointerException - When the supplied query is null.EntityManager.createNativeQuery(String, Class)public static void clear()
IllegalStateException - When the EntityManager is closed.EntityManager.clear()public static void flush()
IllegalStateException - When the EntityManager is closed.javax.persistence.TransactionRequiredException - When no
transaction exists.javax.persistence.PersistenceException - When the flush fails.EntityManager.flush()public static javax.persistence.FlushModeType getFlushMode()
EntityManager.getFlushMode()public static void setFlushMode(javax.persistence.FlushModeType flushMode)
flushMode - The flush mode.IllegalStateException - When the EntityManager is closed.NullPointerException - When the supplied flushMode is null.EntityManager.setFlushMode(FlushModeType)public static void detach(Persistable persistable)
persistable - The persistable to detach.NullPointerException - When the supplied persistable is null.Copyright © 2008–2017. All rights reserved.