Interface DataManager

All Known Implementing Classes:
JpaDataManager

public interface DataManager
This interface, coupled with the JpaDataManager base class, allows for transaction-aware database operations using JPA. See example in DbWorker for details.
Author:
rdoherty
  • Method Summary

    Modifier and Type
    Method
    Description
    <T extends Identifiable>
    void
    deleteObject(T obj)
    Deletes the record represented by the passed object.
    <T extends DataManager>
    void
    doUpdates(DbWorker<T> iface)
    Sets up classes needed to interface with the database, then begins a database transaction and calls doDbTasks() on the passed DbWorker.
    <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.
    <T extends Identifiable>
    T
    saveObject(T obj)
    Saves a JPA entity that implements Identifiable to the database.
  • Method Details

    • doUpdates

      <T extends DataManager> void doUpdates(DbWorker<T> iface) throws Exception
      Sets up classes needed to interface with the database, then begins a database transaction and calls doDbTasks() on the passed DbWorker. When that method returns, ends the transaction, either commiting when successful or rolling back if any exception was thrown from doDbTasks().
      Type Parameters:
      T - implementation of DataManager
      Parameters:
      iface - an instance of a DbWorker for that DataManager type
      Throws:
      Exception - after rolling back the transaction, rethrows any exception that occurs in doDbTasks()
    • saveObject

      <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
      Type Parameters:
      T - class of the object
      Parameters:
      obj - instance of that class
      Returns:
      "attached" instance
    • deleteObject

      <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.
      Type Parameters:
      T - class of object to be removed
      Parameters:
      obj - object to be removed
      Throws:
      IllegalArgumentException - if object passed does not have an ID or does not exist
    • findById

      <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.
      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