Package org.conical.common.bbl.jpa
Class JpaDataManager
java.lang.Object
org.conical.common.bbl.jpa.JpaDataManager
- All Implemented Interfaces:
DataManager
Abstract implementation of DataManager that performs JPA operations within
a transaction. Please note this implementation is NOT thread-safe.
- Author:
- rdoherty
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal <T extends Identifiable>
voiddeleteObject
(T obj) Deletes the record represented by the passed object.final <T extends DataManager>
voidPerforms database operations defined in the passed worker within a transaction.final <T extends Identifiable>
TLooks up an instance of any class implementing Identifiable by its ID and returns it.protected final javax.persistence.EntityManager
getEm()
Returns an open EntityManager with which subclasses can perform JPA operationsprotected abstract javax.persistence.EntityManagerFactory
getEmf()
Returns the EntityManagerFactory that will generate EntityManagers used to perform JPA database operations.final <T extends Identifiable>
TsaveObject
(T obj) Saves a JPA entity that implements Identifiable to the database.
-
Constructor Details
-
JpaDataManager
public JpaDataManager()
-
-
Method Details
-
getEmf
protected abstract javax.persistence.EntityManagerFactory getEmf()Returns the EntityManagerFactory that will generate EntityManagers used to perform JPA database operations.- Returns:
- configured EntityManagerFactory
-
doUpdates
Performs database operations defined in the passed worker within a transaction.- Specified by:
doUpdates
in interfaceDataManager
- Type Parameters:
T
- implementation of DataManager- Parameters:
dbUpdater
- worker which will perform the actual database operations- Throws:
Exception
- after rolling back the transaction, rethrows any exception that occurs in doDbTasks()
-
getEm
protected final javax.persistence.EntityManager getEm()Returns an open EntityManager with which subclasses can perform JPA operations- Returns:
- open EntityManager
- Throws:
IllegalStateException
- if this method is called from anywhere other than inside the doDbTasks method of a subclass of DbWorker
-
findById
Looks up an instance of any class implementing Identifiable by its ID and returns it.- Specified by:
findById
in interfaceDataManager
- Type Parameters:
T
- class of object to be retrieved- Parameters:
obj
- class of object to be retrievedid
- id of object to be retrieved- Returns:
- object retrieved
-
saveObject
Saves a JPA entity that implements Identifiable to the database. The argument will be merged if it represents an existing record (i.e. already has an ID), or persisted if it is a new record (no ID yet). There are a few requirements for this to work:- The object must be a JPA entity
- The object's class must be configured to generate its ID
- Specified by:
saveObject
in interfaceDataManager
- Type Parameters:
T
- class of the object- Parameters:
obj
- instance of that class- Returns:
- "attached" instance
-
deleteObject
Deletes the record represented by the passed object. If the object is detached, it will be looked up using its ID and removed.- Specified by:
deleteObject
in interfaceDataManager
- Type Parameters:
T
- class of object to be removed- Parameters:
obj
- object to be removed
-