ch.elca.el4j.services.persistence.jpa.dao
Class GenericJpaDao<T,ID extends Serializable>

java.lang.Object
  extended by org.springframework.dao.support.DaoSupport
      extended by org.springframework.orm.jpa.support.JpaDaoSupport
          extended by ch.elca.el4j.services.persistence.jpa.dao.ConvenienceJpaDaoSupport
              extended by ch.elca.el4j.services.persistence.jpa.dao.GenericJpaDao<T,ID>
Type Parameters:
T - The domain class the DAO is responsible for
ID - The type of the domain class' identifier
All Implemented Interfaces:
ConvenienceGenericJpaDao<T,ID>, InitializingBean

public class GenericJpaDao<T,ID extends Serializable>
extends ConvenienceJpaDaoSupport
implements ConvenienceGenericJpaDao<T,ID>, InitializingBean

This class is a JPA-specific implementation of the ConvenienceGenericDao interface.

Author:
Simon Stelling (SST)
File-location:
GenericJpaDao
Last check-in date:
2010-12-21 11:08:04 +0100 (Di, 21. Dez 2010) by swismer for revision 4253

Field Summary
 
Fields inherited from class org.springframework.dao.support.DaoSupport
logger
 
Constructor Summary
GenericJpaDao()
          Set up the Generic Dao.
 
Method Summary
 void delete(Collection<T> entities)
          Deletes the given domain objects.
 void delete(ID id)
          Deprecated. 
 void delete(T entity)
          Deletes the given domain object.
 void deleteAll()
          Deletes all available T.
 void deleteById(ID id)
          Deletes the domain object with the given id, disregarding any concurrent modifications that may have occurred.
protected  List<T> fetchExtent(List<T> objects, DataExtent extent)
          Prototype of Extent-based fetching, steps through all the retrieved objects and calls the methods of the extent to ensure loading from db.
protected  T fetchExtent(T object, DataExtent extent)
          Prototype of Extent-based fetching, steps through all the retrieved objects and calls the methods of the extent to ensure loading from db.
 T findById(ID id)
          Retrieves a domain object by identifier.
 T findById(ID id, DataExtent extent)
          Retrieves a domain object by identifier.
 T findByIdLazy(ID id)
          
 List<T> findByQuery(QueryBuilder criteria)
          Retrieves all the domain objects matching the JPA criteria.
 List<T> findByQuery(QueryBuilder criteria, DataExtent extent)
          Retrieves all the domain objects matching the JPA criteria.
 List<T> findByQuery(QueryBuilder criteria, int firstResult, int maxResults)
          Retrieves a range of domain objects matching the JPA criteria.
 List<T> findByQuery(QueryBuilder criteria, int firstResult, int maxResults, DataExtent extent)
          Retrieves a range of domain objects matching the JPA criteria.
 int findCountByQuery(QueryBuilder criteria)
          Retrieves the number of domain objects matching the JPA criteria.
 void flush()
          Sometimes, the way Hibernate handles all the actions in a session is very unbelievable.
 List<T> getAll()
          Retrieves all the domain objects of type T.
 List<T> getAll(DataExtent extent)
          Retrieves all the domain objects of type T.
 ExtentFetcher getExtentFetcher()
           
 javax.persistence.criteria.CriteriaQuery<T> getOrderedCriteria()
          
 Class<T> getPersistentClass()
          Needed because the Java generics throw away this type information.
protected  String getPersistentClassName()
          Returns the simple name of the persistent class this DAO is responsible for.
protected  javax.persistence.criteria.CriteriaQuery<T> makeDistinct(javax.persistence.criteria.CriteriaQuery<T> criteria)
           
 T merge(T entity)
          merge the given entity.
 T persist(T entity)
          persist the given entity.
 T refresh(T entity)
          Re-reads the state of the given domain object from the underlying store.
 T refresh(T entity, DataExtent extent)
          Re-reads the state of the given domain object from the underlying store.
 T reload(T entity)
          Re-reads the state of the given domain object from the undermost store (eg. the database).
 T reload(T entity, DataExtent extent)
          Re-reads the state of the given domain object from the undermost store (eg. the database).
 T saveOrUpdate(T entity)
          Deprecated. 
 T saveOrUpdateAndFlush(T entity)
          Deprecated. 
 void setExtentFetcher(ExtentFetcher extentFetcher)
           
 void setPersistentClass(Class<T> c)
          New: this callback is in general no longer required (the constructor figures the type out itself).
 
Methods inherited from class ch.elca.el4j.services.persistence.jpa.dao.ConvenienceJpaDaoSupport
createJpaTemplate, getConvenienceJpaTemplate, setConvenienceJpaTemplate
 
Methods inherited from class org.springframework.orm.jpa.support.JpaDaoSupport
checkDaoConfig, 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
 
Methods inherited from interface org.springframework.beans.factory.InitializingBean
afterPropertiesSet
 

Constructor Detail

GenericJpaDao

public GenericJpaDao()
Set up the Generic Dao. Auto-derive the parametrized type.

Method Detail

setPersistentClass

public void setPersistentClass(Class<T> c)
New: this callback is in general no longer required (the constructor figures the type out itself).

Specified by:
setPersistentClass in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
c - Mandatory. The domain class this DAO is responsible for.

getPersistentClass

public Class<T> getPersistentClass()
Description copied from interface: ConvenienceGenericJpaDao
Needed because the Java generics throw away this type information.

Specified by:
getPersistentClass in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Returns:
Returns the domain class this DAO is responsible for.

findById

@Transactional(propagation=SUPPORTS,
               readOnly=true)
public T findById(ID id)
           throws DataAccessException,
                  DataRetrievalFailureException
Retrieves a domain object by identifier. This method gets the object from the hibernate cache. It might be that you don't get the actual version that is in the database. If you want the actual version do a refresh() after this method call.

Specified by:
findById in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
id - The id of the domain object to find
Returns:
Returns the found domain object.
Throws:
DataRetrievalFailureException - If no domain object could be found with given id.
DataAccessException - If general data access problem occurred

findByIdLazy

@Transactional(propagation=SUPPORTS,
               readOnly=true)
public T findByIdLazy(ID id)
               throws DataAccessException,
                      DataRetrievalFailureException

Throws:
DataAccessException
DataRetrievalFailureException

getAll

@Transactional(propagation=SUPPORTS,
               readOnly=true)
public List<T> getAll()
               throws DataAccessException
Retrieves all the domain objects of type T.

Specified by:
getAll in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Returns:
The list containing all the domain objects of type T; if no such domain objects exist, an empty list will be returned
Throws:
DataAccessException - If general data access problem occurred

findByQuery

@Transactional(propagation=SUPPORTS,
               readOnly=true)
public List<T> findByQuery(QueryBuilder criteria)
                    throws DataAccessException
Retrieves all the domain objects matching the JPA criteria.

Specified by:
findByQuery in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
criteria - the criteria that the result has to fulfill
Returns:
all object that fulfill the criteria
Throws:
DataAccessException
See Also:
ConvenienceJpaTemplate#findByCriteria(DetachedCriteria)

findByQuery

@Transactional(propagation=SUPPORTS,
               readOnly=true)
public List<T> findByQuery(QueryBuilder criteria,
                                                                    int firstResult,
                                                                    int maxResults)
                    throws DataAccessException
Retrieves a range of domain objects matching the JPA criteria.

Specified by:
findByQuery in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
criteria - the criteria that the result has to fulfill
firstResult - the index of the first result to return
maxResults - the maximum number of results to return
Returns:
the specified subset of object that fulfill the criteria
Throws:
DataAccessException
See Also:
ConvenienceJpaTemplate#findByCriteria(DetachedCriteria, int, int)

findCountByQuery

@Transactional(propagation=SUPPORTS,
               readOnly=true)
public int findCountByQuery(QueryBuilder criteria)
                     throws DataAccessException
Retrieves the number of domain objects matching the JPA criteria.

Specified by:
findCountByQuery in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
criteria - the criteria that the result has to fulfill
Returns:
the number of objects that fulfill the criteria
Throws:
DataAccessException
See Also:
ConvenienceJpaTemplate#findCountByCriteria(DetachedCriteria)

merge

@Transactional(propagation=REQUIRED)
public T merge(T entity)
        throws DataAccessException,
               DataIntegrityViolationException,
               OptimisticLockingFailureException
merge the given entity.

Specified by:
merge in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
entity - the entity to merge.
Returns:
the merged entity
Throws:
DataAccessException
DataIntegrityViolationException
OptimisticLockingFailureException

persist

@Transactional(propagation=REQUIRED)
public T persist(T entity)
          throws DataAccessException,
                 DataIntegrityViolationException,
                 OptimisticLockingFailureException
persist the given entity.

Specified by:
persist in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
entity - the entity to persist.
Returns:
the persisted entity
Throws:
DataAccessException
DataIntegrityViolationException
OptimisticLockingFailureException

saveOrUpdate

@Deprecated
public T saveOrUpdate(T entity)
Deprecated. 

Deprecated: Use merge instead. Note: this method is NOT equivalent to Hibernate's saveOrUpdate but to saveOrUpdateCopy, i.e. you need to use the return value:
 dom = dao.saveOrUpdate(dom);
 

Specified by:
saveOrUpdate in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
entity - The domain object to save or update
Returns:
The saved or updated domain object

saveOrUpdateAndFlush

@Deprecated
@Transactional(propagation=REQUIRED)
public T saveOrUpdateAndFlush(T entity)
                       throws DataAccessException,
                              DataIntegrityViolationException,
                              OptimisticLockingFailureException
Deprecated. 

Convenience method: Executes saveOrUpdate() and flush() on that entity.

Specified by:
saveOrUpdateAndFlush in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
entity - The domain object to save or update
Returns:
The saved or updated object
Throws:
DataAccessException
DataIntegrityViolationException
OptimisticLockingFailureException

delete

@Transactional(propagation=REQUIRED)
public void delete(T entity)
            throws DataAccessException
Deletes the given domain object.

Specified by:
delete in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
entity - The domain object to delete
Throws:
OptimisticLockingFailureException - If domain object has been modified/deleted in the meantime
DataAccessException - If general data access problem occurred

refresh

@Transactional(propagation=SUPPORTS,
               readOnly=true)
public T refresh(T entity)
          throws DataAccessException,
                 DataRetrievalFailureException
Re-reads the state of the given domain object from the underlying store.

Specified by:
refresh in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
entity - The domain object to re-read the state of
Returns:
The refreshed entity
Throws:
DataAccessException - If general data access problem occurred
DataRetrievalFailureException - If domain object could not be re-read

delete

@Deprecated
@Transactional(propagation=REQUIRED)
public void delete(ID id)
            throws DataAccessException
Deprecated. 

Throws:
DataAccessException

deleteById

@Transactional(propagation=REQUIRED)
public void deleteById(ID id)
                throws DataAccessException
Deletes the domain object with the given id, disregarding any concurrent modifications that may have occurred.

Specified by:
deleteById in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
id - The id of the domain object to delete
Throws:
OptimisticLockingFailureException - If domain object has been deleted in the meantime
DataAccessException - If general data access problem occurred

delete

@Transactional(propagation=REQUIRED)
public void delete(Collection<T> entities)
            throws DataAccessException,
                   DataIntegrityViolationException,
                   OptimisticLockingFailureException
Deletes the given domain objects. This method executed in a single transaction (by default with the Required semantics).

Specified by:
delete in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
entities - The domain objects to delete.
Throws:
DataAccessException - If general data access problem occurred
OptimisticLockingFailureException - If domain object has been modified/deleted in the meantime
DataIntegrityViolationException

deleteAll

@Transactional(propagation=REQUIRED)
public void deleteAll()
               throws OptimisticLockingFailureException,
                      DataAccessException
Deletes all available T.

Specified by:
deleteAll in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Throws:
OptimisticLockingFailureException - If domain object has been modified/deleted in the meantime
DataAccessException - If general data access problem occurred

flush

@Transactional(propagation=REQUIRED)
public void flush()
Sometimes, the way Hibernate handles all the actions in a session is very unbelievable. For example, we call delete(project); project.setId(null) <= to insert new one insert(project); It could cause java.sql.BatchUpdateException: ORA-00001: unique constraint BECAUSE Hibernate doesn't flush the previous action first. This method provides a way to flush manually some action. Note that this method is only used in an extremely rare case.

Specified by:
flush in interface ConvenienceGenericJpaDao<T,ID extends Serializable>

getOrderedCriteria

public javax.persistence.criteria.CriteriaQuery<T> getOrderedCriteria()


getPersistentClassName

protected String getPersistentClassName()
Returns the simple name of the persistent class this DAO is responsible for.

Returns:
The simple name of the persistent class this DAO is responsible for.

makeDistinct

protected javax.persistence.criteria.CriteriaQuery<T> makeDistinct(javax.persistence.criteria.CriteriaQuery<T> criteria)
Parameters:
criteria - the criteria to modify
Returns:
the criteria enhanced with distinct restrictions

reload

@Transactional(propagation=SUPPORTS,
               readOnly=true)
public T reload(T entity)
         throws DataAccessException,
                DataRetrievalFailureException
Re-reads the state of the given domain object from the undermost store (eg. the database).

Specified by:
reload in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
entity - The domain object to re-load the state of
Returns:
The reloaded entity
Throws:
DataAccessException - If general data access problem occurred
DataRetrievalFailureException - If domain object could not be re-loaded

getExtentFetcher

public ExtentFetcher getExtentFetcher()
Returns:
Returns the extentFetcher.

setExtentFetcher

public void setExtentFetcher(ExtentFetcher extentFetcher)
Parameters:
extentFetcher - Is the extentFetcher to set.

fetchExtent

protected List<T> fetchExtent(List<T> objects,
                              DataExtent extent)
                       throws DataAccessException
Prototype of Extent-based fetching, steps through all the retrieved objects and calls the methods of the extent to ensure loading from db.

Parameters:
objects - list of objects to load in given extent
extent - the fetch-extent
Returns:
returns the new list of objects.
Throws:
DataAccessException

fetchExtent

protected T fetchExtent(T object,
                        DataExtent extent)
                 throws DataAccessException
Prototype of Extent-based fetching, steps through all the retrieved objects and calls the methods of the extent to ensure loading from db.

Parameters:
object - object to load in given extent
extent - the fetch-extent
Returns:
returns the new object.
Throws:
DataAccessException

findByQuery

public List<T> findByQuery(QueryBuilder criteria,
                           DataExtent extent)
                    throws DataAccessException
Retrieves all the domain objects matching the JPA criteria. Loads at least the given extent.

Specified by:
findByQuery in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
criteria - the criteria that the result has to fulfill
extent - the extent in which objects get loaded.
Returns:
all object that fulfill the criteria
Throws:
DataAccessException
See Also:
ConvenienceJpaTemplate#findByCriteria(DetachedCriteria)

findByQuery

public List<T> findByQuery(QueryBuilder criteria,
                           int firstResult,
                           int maxResults,
                           DataExtent extent)
                    throws DataAccessException
Retrieves a range of domain objects matching the JPA criteria. Loads at least the given extent.

Specified by:
findByQuery in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
criteria - the criteria that the result has to fulfill
firstResult - the index of the first result to return
maxResults - the maximum number of results to return
extent - the extent in which objects get loaded.
Returns:
the specified subset of object that fulfill the criteria
Throws:
DataAccessException
See Also:
ConvenienceJpaTemplate#findByCriteria(DetachedCriteria, int, int)

findById

public T findById(ID id,
                  DataExtent extent)
           throws DataRetrievalFailureException,
                  DataAccessException
Retrieves a domain object by identifier. This method gets the object from the hibernate cache. It might be that you don't get the actual version that is in the database. If you want the actual version do a refresh() after this method call. Loads at least the given extent.

Specified by:
findById in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
id - The id of the domain object to find
extent - the extent in which objects get loaded.
Returns:
Returns the found domain object.
Throws:
DataRetrievalFailureException - If no domain object could be found with given id.
DataAccessException - If general data access problem occurred

getAll

public List<T> getAll(DataExtent extent)
               throws DataAccessException
Retrieves all the domain objects of type T. Loads at least the given extent.

Specified by:
getAll in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
extent - the extent in which objects get loaded.
Returns:
The list containing all the domain objects of type T; if no such domain objects exist, an empty list will be returned
Throws:
DataAccessException - If general data access problem occurred

refresh

public T refresh(T entity,
                 DataExtent extent)
          throws DataAccessException,
                 DataRetrievalFailureException
Re-reads the state of the given domain object from the underlying store. Loads at least the given extent.

Specified by:
refresh in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
entity - The domain object to re-read the state of
extent - the extent in which objects get loaded.
Returns:
The refreshed entity
Throws:
DataAccessException - If general data access problem occurred
DataRetrievalFailureException - If domain object could not be re-read

reload

public T reload(T entity,
                DataExtent extent)
         throws DataAccessException,
                DataRetrievalFailureException
Re-reads the state of the given domain object from the undermost store (eg. the database). Loads at least the given extent.

Specified by:
reload in interface ConvenienceGenericJpaDao<T,ID extends Serializable>
Parameters:
entity - The domain object to re-read the state of
extent - the extent in which objects get loaded.
Returns:
The refreshed entity
Throws:
DataAccessException - If general data access problem occurred
DataRetrievalFailureException - If domain object could not be re-read


Copyright © 2005-2011 ELCA. All Rights Reserved.