Package play.db.jpa
Class JPA
- java.lang.Object
-
- play.db.jpa.JPA
-
public class JPA extends java.lang.Object
JPA Support
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JPA.JPAContext
-
Field Summary
Fields Modifier and Type Field Description static java.lang.ThreadLocal<java.util.Map<java.lang.String,JPA.JPAContext>>
currentEntityManager
static java.lang.String
DEFAULT
protected static java.util.Map<java.lang.String,javax.persistence.EntityManagerFactory>
emfs
-
Constructor Summary
Constructors Constructor Description JPA()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
bindForCurrentThread(java.lang.String name, javax.persistence.EntityManager em, boolean readonly)
Bind an EntityManager to the current thread.static void
closeTx(java.lang.String name)
static javax.persistence.EntityManager
createEntityManager()
static javax.persistence.EntityManager
createEntityManager(java.lang.String name)
static javax.persistence.EntityManager
em()
static javax.persistence.EntityManager
em(java.lang.String key)
Get the EntityManager for specified persistence unit for this thread.static int
execute(java.lang.String query)
Execute a JPQL querystatic int
execute(java.lang.String em, java.lang.String query)
static java.lang.String
getDBName(java.lang.Class<?> clazz)
static boolean
isEnabled()
static boolean
isEnabled(java.lang.String em)
static boolean
isInitialized()
static boolean
isInsideTransaction()
static boolean
isInsideTransaction(java.lang.String name)
static javax.persistence.EntityManager
newEntityManager()
static javax.persistence.EntityManager
newEntityManager(java.lang.String key)
static void
rollbackTx(java.lang.String name)
static void
setRollbackOnly()
static void
setRollbackOnly(java.lang.String em)
static void
startTx(java.lang.String name, boolean readOnly)
initialize the JPA context and starts a JPA transactionstatic void
unbindForCurrentThread(java.lang.String name)
static <T> T
withinFilter(F.Function0<T> block)
static <T> T
withTransaction(java.lang.String dbName, boolean readOnly, F.Function0<T> block)
Run a block of code in a JPA transaction.
-
-
-
Field Detail
-
emfs
protected static java.util.Map<java.lang.String,javax.persistence.EntityManagerFactory> emfs
-
currentEntityManager
public static final java.lang.ThreadLocal<java.util.Map<java.lang.String,JPA.JPAContext>> currentEntityManager
-
DEFAULT
public static java.lang.String DEFAULT
-
-
Method Detail
-
isInitialized
public static boolean isInitialized()
-
newEntityManager
public static javax.persistence.EntityManager newEntityManager(java.lang.String key)
-
em
public static javax.persistence.EntityManager em(java.lang.String key)
Get the EntityManager for specified persistence unit for this thread.- Parameters:
key
- The DB name- Returns:
- The EntityManager
-
bindForCurrentThread
public static void bindForCurrentThread(java.lang.String name, javax.persistence.EntityManager em, boolean readonly)
Bind an EntityManager to the current thread.- Parameters:
name
- The DB nameem
- The EntityManagerreadonly
- indicate if it is in read only mode
-
unbindForCurrentThread
public static void unbindForCurrentThread(java.lang.String name)
-
em
public static javax.persistence.EntityManager em()
-
setRollbackOnly
public static void setRollbackOnly()
-
setRollbackOnly
public static void setRollbackOnly(java.lang.String em)
-
isEnabled
public static boolean isEnabled()
- Returns:
- true if an entityManagerFactory has started
-
isEnabled
public static boolean isEnabled(java.lang.String em)
-
execute
public static int execute(java.lang.String query)
Execute a JPQL query- Parameters:
query
- The query to execute- Returns:
- The result code
-
execute
public static int execute(java.lang.String em, java.lang.String query)
-
newEntityManager
public static javax.persistence.EntityManager newEntityManager()
-
createEntityManager
public static javax.persistence.EntityManager createEntityManager()
-
createEntityManager
public static javax.persistence.EntityManager createEntityManager(java.lang.String name)
-
isInsideTransaction
public static boolean isInsideTransaction()
- Returns:
- true if current thread is running inside a transaction
-
isInsideTransaction
public static boolean isInsideTransaction(java.lang.String name)
-
withinFilter
public static <T> T withinFilter(F.Function0<T> block) throws java.lang.Throwable
- Throws:
java.lang.Throwable
-
getDBName
public static java.lang.String getDBName(java.lang.Class<?> clazz)
-
withTransaction
public static <T> T withTransaction(java.lang.String dbName, boolean readOnly, F.Function0<T> block) throws java.lang.Throwable
Run a block of code in a JPA transaction.- Type Parameters:
T
- The entity class- Parameters:
dbName
- The persistence unit namereadOnly
- Is the transaction read-only?block
- Block of code to execute.- Returns:
- The result
- Throws:
java.lang.Throwable
- Thrown in case of error
-
startTx
public static void startTx(java.lang.String name, boolean readOnly)
initialize the JPA context and starts a JPA transaction- Parameters:
name
- The persistence unit namereadOnly
- true for a readonly transaction
-
closeTx
public static void closeTx(java.lang.String name)
-
rollbackTx
public static void rollbackTx(java.lang.String name)
-
-