Class JpaDataManager

java.lang.Object
org.conical.common.bbl.jpa.JpaDataManager
All Implemented Interfaces:
DataManager

public abstract class JpaDataManager extends Object implements DataManager
Abstract implementation of DataManager that performs JPA operations within a transaction. Please note this implementation is NOT thread-safe.
Author:
rdoherty
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final <T extends Identifiable>
    void
    deleteObject(T obj)
    Deletes the record represented by the passed object.
    final <T extends DataManager>
    void
    doUpdates(DbWorker<T> dbUpdater)
    Performs database operations defined in the passed worker within a transaction.
    final <T extends Identifiable>
    T
    findById(Class<T> obj, Long id)
    Looks up an instance of any class implementing Identifiable by its ID and returns it.
    protected final javax.persistence.EntityManager
    Returns an open EntityManager with which subclasses can perform JPA operations
    protected abstract javax.persistence.EntityManagerFactory
    Returns the EntityManagerFactory that will generate EntityManagers used to perform JPA database operations.
    final <T extends Identifiable>
    T
    saveObject(T obj)
    Saves a JPA entity that implements Identifiable to the database.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public final <T extends DataManager> void doUpdates(DbWorker<T> dbUpdater) throws Exception
      Performs database operations defined in the passed worker within a transaction.
      Specified by:
      doUpdates in interface DataManager
      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

      public final <T extends Identifiable> T findById(Class<T> obj, Long id)
      Looks up an instance of any class implementing Identifiable by its ID and returns it.
      Specified by:
      findById in interface DataManager
      Type Parameters:
      T - class of object to be retrieved
      Parameters:
      obj - class of object to be retrieved
      id - id of object to be retrieved
      Returns:
      object retrieved
    • saveObject

      public final <T extends Identifiable> T saveObject(T obj)
      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 interface DataManager
      Type Parameters:
      T - class of the object
      Parameters:
      obj - instance of that class
      Returns:
      "attached" instance
    • deleteObject

      public final <T extends Identifiable> void deleteObject(T obj)
      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 interface DataManager
      Type Parameters:
      T - class of object to be removed
      Parameters:
      obj - object to be removed