ch.elca.el4j.services.persistence.hibernate.dao
Class ConvenienceHibernateTemplate

java.lang.Object
  extended by org.springframework.orm.hibernate3.HibernateAccessor
      extended by org.springframework.orm.hibernate3.HibernateTemplate
          extended by ch.elca.el4j.services.persistence.hibernate.dao.ConvenienceHibernateTemplate
All Implemented Interfaces:
BeanFactoryAware, InitializingBean, HibernateOperations

public class ConvenienceHibernateTemplate
extends HibernateTemplate

This is a convenience class for the Hibernate template. Features:

Author:
Alex Mathey (AMA)
File-location:
ConvenienceHibernateTemplate
Last check-in date:
2010-08-04 14:40:16 +0200 (Mi, 04. Aug 2010) by swrelca for revision 4110

Field Summary
 
Fields inherited from class org.springframework.orm.hibernate3.HibernateAccessor
FLUSH_ALWAYS, FLUSH_AUTO, FLUSH_COMMIT, FLUSH_EAGER, FLUSH_NEVER, logger
 
Constructor Summary
ConvenienceHibernateTemplate(SessionFactory sessionFactory)
          Constructor.
 
Method Summary
 void createHibernateSearchIndex(Collection<?> objects)
          Trigger Hibernate Search index process explicitly.
 void deleteStrong(Class<?> entityClass, Serializable id, String objectName)
          Deletes the persistent instance given by its identifier in a strong way: first, the persistent instance is retrieved with the help of the identifier.
 Object findByNamedParamStrong(String queryString, String paramName, Object value, String objectName)
          Retrieves a persistent instance with the help of a parameterized query: does the same as the findByNamedParam(String, String, Object) method, but returns a persistent instance instead of a list of persistent objects and throws a DataRetrievalException if the returned list does not contain exactly one element.
 int findCountByCriteria(DetachedCriteria criteria)
          Counts the number of results of a search.
 Object getByIdStrong(Class<?> entityClass, Serializable id, String objectName)
          Retrieves the persistent instance given by its identifier in a strong way: does the same as the getById(Class, java.io.Serializable) method, but throws a DataRetrievalException instead of null if the persistent instance could not be found.
 Object getByIdStrongLazy(Class<?> entityClass, Serializable id, String objectName)
          Retrieves a persistent instance lazily.
 int getFirstResult()
          Gets the id of the first result to return.
protected  void prepareCriteria(Criteria criteria)
          
protected  void prepareQuery(Query queryObject)
          Overload parent class to support also a constraint of the id of the first result to load.
 void saveOrUpdateStrong(Object entity, String objectName)
          Saves or updates the given persistent instance in a strong way: does the same as the saveOrUpdate(Object) method, but throws a more specific OptimisticLockingFailureException in the case of an optimistic locking failure.
<T> List<T>
search(Class<T> entityClass, String[] fields, String searchString)
          Search for objects having fields that match the search string.
 void setFirstResult(int firstResult)
          Sets the id of the first result to return.
 
Methods inherited from class org.springframework.orm.hibernate3.HibernateTemplate
applyNamedParameterToQuery, bulkUpdate, bulkUpdate, bulkUpdate, checkWriteOperationAllowed, clear, closeIterator, contains, createSessionProxy, delete, delete, delete, delete, deleteAll, doExecute, enableFilter, evict, execute, executeFind, executeWithNativeSession, executeWithNewSession, find, find, find, findByCriteria, findByCriteria, findByExample, findByExample, findByExample, findByExample, findByNamedParam, findByNamedParam, findByNamedQuery, findByNamedQuery, findByNamedQuery, findByNamedQueryAndNamedParam, findByNamedQueryAndNamedParam, findByNamedQueryAndValueBean, findByValueBean, flush, get, get, get, get, getFetchSize, getMaxResults, getQueryCacheRegion, getSession, initialize, isAllowCreate, isAlwaysUseNewSession, isCacheQueries, isCheckWriteOperations, isExposeNativeSession, iterate, iterate, iterate, load, load, load, load, load, loadAll, lock, lock, merge, merge, persist, persist, refresh, refresh, replicate, replicate, save, save, saveOrUpdate, saveOrUpdate, saveOrUpdateAll, setAllowCreate, setAlwaysUseNewSession, setCacheQueries, setCheckWriteOperations, setExposeNativeSession, setFetchSize, setMaxResults, setQueryCacheRegion, update, update, update, update
 
Methods inherited from class org.springframework.orm.hibernate3.HibernateAccessor
afterPropertiesSet, applyFlushMode, convertHibernateAccessException, convertJdbcAccessException, convertJdbcAccessException, disableFilters, enableFilters, flushIfNecessary, getDefaultJdbcExceptionTranslator, getEntityInterceptor, getFilterNames, getFlushMode, getJdbcExceptionTranslator, getSessionFactory, setBeanFactory, setEntityInterceptor, setEntityInterceptorBeanName, setFilterName, setFilterNames, setFlushMode, setFlushModeName, setJdbcExceptionTranslator, setSessionFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConvenienceHibernateTemplate

public ConvenienceHibernateTemplate(SessionFactory sessionFactory)
Constructor.

Parameters:
sessionFactory - SessionFactory to create Sessions
Method Detail

getByIdStrong

public Object getByIdStrong(Class<?> entityClass,
                            Serializable id,
                            String objectName)
                     throws DataAccessException,
                            DataRetrievalFailureException
Retrieves the persistent instance given by its identifier in a strong way: does the same as the getById(Class, java.io.Serializable) method, but throws a DataRetrievalException instead of null if the persistent instance could not be found.

Parameters:
entityClass - The class of the object which should be returned.
id - An identifier of the persistent instance
objectName - Name of the persistent object type.
Returns:
the persistent instance
Throws:
DataAccessException - in case of Hibernate errors
DataRetrievalFailureException - in case the persistent instance is null
See Also:
HibernateTemplate.get(Class, java.io.Serializable)

getByIdStrongLazy

public Object getByIdStrongLazy(Class<?> entityClass,
                                Serializable id,
                                String objectName)
                         throws DataAccessException,
                                DataRetrievalFailureException
Retrieves a persistent instance lazily.

Throws:
DataAccessException
DataRetrievalFailureException
See Also:
getByIdStrong

findByNamedParamStrong

public Object findByNamedParamStrong(String queryString,
                                     String paramName,
                                     Object value,
                                     String objectName)
                              throws DataAccessException,
                                     DataRetrievalFailureException
Retrieves a persistent instance with the help of a parameterized query: does the same as the findByNamedParam(String, String, Object) method, but returns a persistent instance instead of a list of persistent objects and throws a DataRetrievalException if the returned list does not contain exactly one element.

Parameters:
queryString - The string corresponding to HQL query
paramName - The name of the parameter
value - The value of the parameter
objectName - Name of the persistent object type.
Returns:
the persistent instance returned by the query
Throws:
DataAccessException - in case of Hibernate errors
DataRetrievalFailureException - in case the list of persistent instances is empty, or if it contains more than one object
See Also:
HibernateTemplate.findByNamedParam(String, String, Object)

saveOrUpdateStrong

public void saveOrUpdateStrong(Object entity,
                               String objectName)
                        throws DataAccessException,
                               OptimisticLockingFailureException
Saves or updates the given persistent instance in a strong way: does the same as the saveOrUpdate(Object) method, but throws a more specific OptimisticLockingFailureException in the case of an optimistic locking failure.

Parameters:
entity - the persistent entity to save or update
objectName - Name of the persistent object type.
Throws:
DataAccessException - in case of Hibernate errors
OptimisticLockingFailureException - in case optimistic locking fails
See Also:
HibernateTemplate.saveOrUpdate(Object)

deleteStrong

public void deleteStrong(Class<?> entityClass,
                         Serializable id,
                         String objectName)
                  throws DataRetrievalFailureException
Deletes the persistent instance given by its identifier in a strong way: first, the persistent instance is retrieved with the help of the identifier. If it exists, it will be deleted, otherwise a DataRetrievalFailureException will be thrown.

Parameters:
entityClass - The class of the object which should be deleted.
id - The identifier of the persistent instance to delete
objectName - Name of the persistent object type.
Throws:
DataRetrievalFailureException - in case the persistent instance to delete is null
See Also:
HibernateTemplate.delete(Object)

prepareQuery

protected void prepareQuery(Query queryObject)
Overload parent class to support also a constraint of the id of the first result to load. TODO shall we drop this and instead use the findByCriteria(DetachedCriteria,int,int) method?

Overrides:
prepareQuery in class HibernateTemplate

prepareCriteria

protected void prepareCriteria(Criteria criteria)

Overrides:
prepareCriteria in class HibernateTemplate

findCountByCriteria

public int findCountByCriteria(DetachedCriteria criteria)
                        throws DataAccessException
Counts the number of results of a search.

Parameters:
criteria - The criteria for the query.
Returns:
The number of results of the query.
Throws:
DataAccessException

getFirstResult

public int getFirstResult()
Gets the id of the first result to return.

Returns:
The id of the first result to return.

setFirstResult

public void setFirstResult(int firstResult)
Sets the id of the first result to return.

Parameters:
firstResult - The id of the first result to return.

createHibernateSearchIndex

public void createHibernateSearchIndex(Collection<?> objects)
                                throws DataAccessException,
                                       DataRetrievalFailureException
Trigger Hibernate Search index process explicitly.

Parameters:
objects - objects to index
Throws:
DataAccessException
DataRetrievalFailureException

search

public <T> List<T> search(Class<T> entityClass,
                          String[] fields,
                          String searchString)
               throws DataAccessException,
                      DataRetrievalFailureException
Search for objects having fields that match the search string.

Type Parameters:
T - the type of entities
Parameters:
entityClass - the class of the entities to search
fields - the names of the fields to consider while searching
searchString - the search string
Returns:
all entities having fields that match the search string.
Throws:
DataAccessException
DataRetrievalFailureException


Copyright © 2005-2011 ELCA. All Rights Reserved.