org.dellroad.stuff.dao
Class AbstractDAO<T>

java.lang.Object
  extended by org.springframework.dao.support.DaoSupport
      extended by org.springframework.orm.jpa.support.JpaDaoSupport
          extended by org.dellroad.stuff.dao.AbstractDAO<T>
Type Parameters:
T - persistent instance type
All Implemented Interfaces:
DAO<T>, InitializingBean

public abstract class AbstractDAO<T>
extends JpaDaoSupport
implements DAO<T>

Support superclass for JPA DAO implementations.


Nested Class Summary
protected  class AbstractDAO.DAOCriteriaListCallback
          Convenience subclass of CriteriaCallback for use by DAO subclasses when returning lists of persistent instances.
protected  class AbstractDAO.DAOCriteriaUniqueCallback
          Convenience subclass of CriteriaCallback for use by DAO subclasses when returning a single persistent instance.
protected  class AbstractDAO.DAOQueryListCallback
          Convenience subclass of QueryCallback for use by DAO subclasses when returning lists of persistent instances.
protected  class AbstractDAO.DAOQueryUniqueCallback
          Convenience subclass of QueryCallback for use by DAO subclasses when returning a single persistent instance.
 
Field Summary
protected  Logger log
           
protected  Class<T> type
          Persistent instance type.
 
Fields inherited from class org.springframework.dao.support.DaoSupport
logger
 
Constructor Summary
protected AbstractDAO(Class<T> type)
          Constructor.
protected AbstractDAO(Class<T> type, EntityManagerFactory entityManagerFactory)
          Constructor.
protected AbstractDAO(Class<T> type, JpaTemplate jpaTemplate)
          Constructor.
 
Method Summary
protected  int bulkUpdate(UpdateCallback callback)
          Perform a bulk update.
protected  T cast(Object obj)
          Cast the given object to this instance's persistent instance type.
protected  List<T> castList(List<?> list)
          Cast the given list to a list of this instance's persistent instance type.
 void clear()
          Clear the session cache.
 void delete(T obj)
          Delete the given instance from the persistent store.
 void detach(Object obj)
          Evict an object from the session cache.
protected  List<T> find(String queryString, Object... params)
          Find instances using a query string and query parameters.
protected  T findUnique(String queryString, Object... params)
          Find a unique instance using a query string and query parameters.
 void flush()
          Flush outstanding changes to the persistent store.
 List<T> getAll()
          Get all instances.
protected
<R> R
getBy(QueryCallback<R> callback)
          Search using a QueryCallback.
 T getById(long id)
          Get an instance by ID.
 T getReference(long id)
          Get a reference to an instance by ID.
 boolean isReadOnly()
          Determine if the current transaction is read-only.
 T merge(T obj)
          Merge the given object into the current session.
 void refresh(T obj)
          Refresh the given object from the database.
 void save(T obj)
          Save a newly created instance.
 void setFlushMode(FlushModeType flushMode)
          Set flush mode.
 
Methods inherited from class org.springframework.orm.jpa.support.JpaDaoSupport
checkDaoConfig, createJpaTemplate, createJpaTemplate, getJpaTemplate, setEntityManager, setEntityManagerFactory, setJpaTemplate
 
Methods inherited from class org.springframework.dao.support.DaoSupport
afterPropertiesSet, initDao
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected final Logger log

type

protected final Class<T> type
Persistent instance type.

Constructor Detail

AbstractDAO

protected AbstractDAO(Class<T> type)
Constructor.

Parameters:
type - persistent instance type

AbstractDAO

protected AbstractDAO(Class<T> type,
                      EntityManagerFactory entityManagerFactory)
Constructor.

Parameters:
type - persistent instance type
entityManagerFactory - EntityManagerFactory from which to create the JpaTemplate used by this instance

AbstractDAO

protected AbstractDAO(Class<T> type,
                      JpaTemplate jpaTemplate)
Constructor.

Parameters:
type - persistent instance type
jpaTemplate - JpaTemplate to be used by this instance
Method Detail

getById

public T getById(long id)
Description copied from interface: DAO
Get an instance by ID. This assumes object IDs are long values.

Specified by:
getById in interface DAO<T>

getAll

public List<T> getAll()
Description copied from interface: DAO
Get all instances.

Specified by:
getAll in interface DAO<T>

getReference

public T getReference(long id)
Description copied from interface: DAO
Get a reference to an instance by ID. This assumes object IDs are long values.

Note if the instance does not exist, then an exception may be thrown either here or later upon first access.

Specified by:
getReference in interface DAO<T>

find

protected List<T> find(String queryString,
                       Object... params)
Find instances using a query string and query parameters.


findUnique

protected T findUnique(String queryString,
                       Object... params)
Find a unique instance using a query string and query parameters.

Returns:
unique instance found, or null if none was found

getBy

protected <R> R getBy(QueryCallback<R> callback)
Search using a QueryCallback.


bulkUpdate

protected int bulkUpdate(UpdateCallback callback)
Perform a bulk update.


save

public void save(T obj)
Description copied from interface: DAO
Save a newly created instance.

Specified by:
save in interface DAO<T>

delete

public void delete(T obj)
Description copied from interface: DAO
Delete the given instance from the persistent store.

Specified by:
delete in interface DAO<T>

merge

public T merge(T obj)
Description copied from interface: DAO
Merge the given object into the current session.

Specified by:
merge in interface DAO<T>

refresh

public void refresh(T obj)
Description copied from interface: DAO
Refresh the given object from the database.

Specified by:
refresh in interface DAO<T>

detach

public void detach(Object obj)
Description copied from interface: DAO
Evict an object from the session cache.

Specified by:
detach in interface DAO<T>

flush

public void flush()
Description copied from interface: DAO
Flush outstanding changes to the persistent store.

Specified by:
flush in interface DAO<T>

setFlushMode

public void setFlushMode(FlushModeType flushMode)
Description copied from interface: DAO
Set flush mode.

Specified by:
setFlushMode in interface DAO<T>

clear

public void clear()
Description copied from interface: DAO
Clear the session cache.

Specified by:
clear in interface DAO<T>

isReadOnly

public boolean isReadOnly()
Description copied from interface: DAO
Determine if the current transaction is read-only.

Specified by:
isReadOnly in interface DAO<T>
Returns:
true if the current transaction is read-only

cast

protected T cast(Object obj)
Cast the given object to this instance's persistent instance type.


castList

protected List<T> castList(List<?> list)
Cast the given list to a list of this instance's persistent instance type. Does not actually inspect the contents of the list.