|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.orm.hibernate.HibernateAccessor org.springframework.orm.hibernate.HibernateTemplate
Helper class that simplifies Hibernate data access code, and converts checked HibernateExceptions into unchecked DataAccessExceptions, compatible to the org.springframework.dao exception hierarchy. Uses the same SQLExceptionTranslator mechanism as JdbcTemplate.
Typically used to implement data access or business logic services that use Hibernate within their implementation but are Hibernate-agnostic in their interface. The latter resp. code calling the latter only have to deal with business objects, query objects, and org.springframework.dao exceptions.
The central method is "execute", supporting Hibernate code implementing the HibernateCallback interface. It provides Hibernate Session handling such that neither the HibernateCallback implementation nor the calling code needs to explicitly care about retrieving/closing Hibernate Sessions, or handling Session lifecycle exceptions. For typical single step actions, there are various convenience methods (find, load, saveOrUpdate, delete).
Can be used within a service implementation via direct instantiation with a SessionFactory reference, or get prepared in an application context and given to services as bean reference. Note: The SessionFactory should always be configured as bean in the application context, in the first case given to the service directly, in the second case to the prepared template.
This class can be considered a programmatic alternative to HibernateInterceptor. The major advantage is its straightforwardness, the major disadvantage that no checked application exceptions can get thrown from within data access code. Respective checks and the actual throwing of such exceptions can often be deferred to after callback execution, though.
Note that even if HibernateTransactionManager is used for transaction demarcation in higher-level services, all those services above the data access layer don't need need to be Hibernate-aware. Setting such a special PlatformTransactionManager is a configuration issue, without introducing code dependencies. For example, switching to JTA is just a matter of Spring configuration (use JtaTransactionManager instead), without needing to touch application code.
LocalSessionFactoryBean is the preferred way of obtaining a reference to a specific Hibernate SessionFactory, at least in a non-EJB environment.
Note: Spring's Hibernate support requires Hibernate 2.1 (as of Spring 1.0).
HibernateCallback
,
HibernateInterceptor
,
HibernateTransactionManager
,
LocalSessionFactoryBean
,
JndiObjectFactoryBean
,
Session
Field Summary |
Fields inherited from class org.springframework.orm.hibernate.HibernateAccessor |
FLUSH_AUTO, FLUSH_EAGER, FLUSH_NEVER, logger |
Constructor Summary | |
HibernateTemplate()
Create a new HibernateTemplate instance. |
|
HibernateTemplate(net.sf.hibernate.SessionFactory sessionFactory)
Create a new HibernateTemplate instance. |
|
HibernateTemplate(net.sf.hibernate.SessionFactory sessionFactory,
boolean allowCreate)
Create a new HibernateTemplate instance. |
Method Summary | |
net.sf.hibernate.Criteria |
createCriteria(net.sf.hibernate.Session session,
java.lang.Class entityClass)
Create a Criteria object for the given Session and the given entity class. |
net.sf.hibernate.Query |
createQuery(net.sf.hibernate.Session session,
java.lang.String queryString)
Create a Query object for the given Session and the given query string. |
void |
delete(java.lang.Object entity)
Delete the given persistent instance. |
void |
delete(java.lang.Object entity,
net.sf.hibernate.LockMode lockMode)
Delete the given persistent instance. |
void |
deleteAll(java.util.Collection entities)
Delete all given persistent instances. |
void |
evict(java.lang.Object entity)
Remove the given object from the Session cache. |
java.lang.Object |
execute(HibernateCallback action)
Execute the action specified by the given action object within a session. |
java.util.List |
executeFind(HibernateCallback action)
Execute the specified action assuming that the result object is a List. |
java.util.List |
find(java.lang.String queryString)
Execute a query for persistent instances. |
java.util.List |
find(java.lang.String queryString,
java.lang.Object value)
Execute a query for persistent instances, binding one value to a "?" parameter in the query string. |
java.util.List |
find(java.lang.String queryString,
java.lang.Object[] values)
Execute a query for persistent instances, binding a number of values to "?" parameters in the query string. |
java.util.List |
find(java.lang.String queryString,
java.lang.Object[] values,
net.sf.hibernate.type.Type[] types)
Execute a query for persistent instances, binding a number of values to "?" parameters of the given types in the query string. |
java.util.List |
find(java.lang.String queryString,
java.lang.Object value,
net.sf.hibernate.type.Type type)
Execute a query for persistent instances, binding one value to a "?" parameter of the given type in the query string. |
java.util.List |
findByNamedQuery(java.lang.String queryName)
Execute a named query for persistent instances. |
java.util.List |
findByNamedQuery(java.lang.String queryName,
java.lang.Object value)
Execute a named query for persistent instances, binding one value to a "?" parameter in the query string. |
java.util.List |
findByNamedQuery(java.lang.String queryName,
java.lang.Object[] values)
Execute a named query for persistent instances, binding a number of values to "?" parameters in the query string. |
java.util.List |
findByNamedQuery(java.lang.String queryName,
java.lang.Object[] values,
net.sf.hibernate.type.Type[] types)
Execute a named query for persistent instances, binding a number of values to "?" parameters in the query string. |
java.util.List |
findByNamedQuery(java.lang.String queryName,
java.lang.Object value,
net.sf.hibernate.type.Type type)
Execute a named query for persistent instances, binding one value to a "?" parameter in the query string. |
java.util.List |
findByNamedQueryAndValueBean(java.lang.String queryName,
java.lang.Object valueBean)
Execute a named query for persistent instances, binding the properties of the given bean to named parameters in the query string. |
java.util.List |
findByValueBean(java.lang.String queryString,
java.lang.Object valueBean)
Execute a query for persistent instances, binding the properties of the given bean to named parameters in the query string. |
java.lang.Object |
get(java.lang.Class entityClass,
java.io.Serializable id)
Return the persistent instance of the given entity class with the given identifier, or null if not found. |
java.lang.Object |
get(java.lang.Class entityClass,
java.io.Serializable id,
net.sf.hibernate.LockMode lockMode)
Return the persistent instance of the given entity class with the given identifier, or null if not found. |
net.sf.hibernate.Query |
getNamedQuery(net.sf.hibernate.Session session,
java.lang.String queryName)
Create a named Query object for the given Session and the given query name. |
boolean |
isAllowCreate()
Return if a new Session should be created if no thread-bound found. |
java.lang.Object |
load(java.lang.Class entityClass,
java.io.Serializable id)
Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found. |
java.lang.Object |
load(java.lang.Class entityClass,
java.io.Serializable id,
net.sf.hibernate.LockMode lockMode)
Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found. |
java.util.List |
loadAll(java.lang.Class entityClass)
Return all persistent instances of the given entity class. |
void |
lock(java.lang.Object entity,
net.sf.hibernate.LockMode lockMode)
Obtain the specified lock level upon the given object, implicitly checking whether the corresponding database entry still exists (throwing an OptimisticLockingFailureException if not found). |
java.io.Serializable |
save(java.lang.Object entity)
Save the given persistent instance. |
void |
save(java.lang.Object entity,
java.io.Serializable id)
Save the given persistent instance with the given identifier. |
void |
saveOrUpdate(java.lang.Object entity)
Save respectively update the given persistent instance, according to its ID (matching the configured "unsaved-value"?). |
java.lang.Object |
saveOrUpdateCopy(java.lang.Object entity)
Save respectively update the contents of given persistent object, according to its ID (matching the configured "unsaved-value"?). |
void |
setAllowCreate(boolean allowCreate)
Set if a new Session should be created if no thread-bound found. |
void |
update(java.lang.Object entity)
Update the given persistent instance. |
void |
update(java.lang.Object entity,
net.sf.hibernate.LockMode lockMode)
Update the given persistent instance. |
Methods inherited from class org.springframework.orm.hibernate.HibernateAccessor |
afterPropertiesSet, convertHibernateAccessException, convertJdbcAccessException, flushIfNecessary, getEntityInterceptor, getFlushMode, getJdbcExceptionTranslator, getSessionFactory, setEntityInterceptor, setFlushMode, setFlushModeName, setJdbcExceptionTranslator, setSessionFactory |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public HibernateTemplate()
public HibernateTemplate(net.sf.hibernate.SessionFactory sessionFactory)
sessionFactory
- SessionFactory to create Sessionspublic HibernateTemplate(net.sf.hibernate.SessionFactory sessionFactory, boolean allowCreate)
sessionFactory
- SessionFactory to create SessionsallowCreate
- if a new Session should be created
if no thread-bound foundMethod Detail |
public void setAllowCreate(boolean allowCreate)
HibernateTemplate is aware of a respective Session bound to the current thread, for example when using HibernateTransactionManager. If allowCreate is true, a new Session will be created if none found. If false, an IllegalStateException will get thrown in this case.
SessionFactoryUtils.getSession(SessionFactory, boolean)
public boolean isAllowCreate()
public java.lang.Object execute(HibernateCallback action) throws DataAccessException
HibernateOperations
Note: Callback code is not supposed to handle transactions itself! Use an appropriate transaction manager like HibernateTransactionManager. Generally, callback code must not touch any Session lifecycle methods, like close, disconnect, or reconnect, to let the template do its work.
execute
in interface HibernateOperations
action
- callback object that specifies the Hibernate action
DataAccessException
- in case of Hibernate errorsHibernateTransactionManager
,
org.springframework.dao
,
org.springframework.transaction
public java.util.List executeFind(HibernateCallback action) throws DataAccessException
HibernateOperations
This is a convenience method for executing Hibernate find calls within an action.
executeFind
in interface HibernateOperations
action
- action object that specifies the Hibernate action
DataAccessException
- in case of Hibernate errorspublic java.lang.Object get(java.lang.Class entityClass, java.io.Serializable id) throws DataAccessException
HibernateOperations
get
in interface HibernateOperations
entityClass
- a persistent classid
- an identifier of the persistent instance
DataAccessException
- in case of Hibernate errorsSession.get(Class, java.io.Serializable)
public java.lang.Object get(java.lang.Class entityClass, java.io.Serializable id, net.sf.hibernate.LockMode lockMode) throws DataAccessException
HibernateOperations
get
in interface HibernateOperations
entityClass
- a persistent classid
- an identifier of the persistent instance
DataAccessException
- in case of Hibernate errorsSession.get(Class, java.io.Serializable, net.sf.hibernate.LockMode)
public java.lang.Object load(java.lang.Class entityClass, java.io.Serializable id) throws DataAccessException
HibernateOperations
load
in interface HibernateOperations
entityClass
- a persistent classid
- an identifier of the persistent instance
DataAccessException
- in case of Hibernate errorsSession.load(Class, java.io.Serializable)
public java.lang.Object load(java.lang.Class entityClass, java.io.Serializable id, net.sf.hibernate.LockMode lockMode) throws DataAccessException
HibernateOperations
load
in interface HibernateOperations
entityClass
- a persistent classid
- an identifier of the persistent instance
DataAccessException
- in case of Hibernate errorsSession.load(Class, java.io.Serializable)
public java.util.List loadAll(java.lang.Class entityClass) throws DataAccessException
HibernateOperations
loadAll
in interface HibernateOperations
entityClass
- a persistent class
DataAccessException
- if there is a Hibernate errorSession.createCriteria(java.lang.Class)
public void evict(java.lang.Object entity) throws DataAccessException
HibernateOperations
evict
in interface HibernateOperations
entity
- the persistent instance to lock
DataAccessException
- in case of Hibernate errorsSession.evict(Object)
public void lock(java.lang.Object entity, net.sf.hibernate.LockMode lockMode) throws DataAccessException
HibernateOperations
lock
in interface HibernateOperations
entity
- the persistent instance to lock
DataAccessException
- in case of Hibernate errorsHibernateOptimisticLockingFailureException
,
Session.lock(Object, net.sf.hibernate.LockMode)
public java.io.Serializable save(java.lang.Object entity) throws DataAccessException
HibernateOperations
save
in interface HibernateOperations
entity
- the persistent instance to save
DataAccessException
- in case of Hibernate errorsSession.save(Object)
public void save(java.lang.Object entity, java.io.Serializable id) throws DataAccessException
HibernateOperations
save
in interface HibernateOperations
entity
- the persistent instance to saveid
- the identifier to assign
DataAccessException
- in case of Hibernate errorsSession.save(Object, java.io.Serializable)
public void saveOrUpdate(java.lang.Object entity) throws DataAccessException
HibernateOperations
saveOrUpdate
in interface HibernateOperations
entity
- the persistent instance to save respectively update
(to be associated with the Hibernate Session)
DataAccessException
- in case of Hibernate errorsSession.saveOrUpdate(Object)
public java.lang.Object saveOrUpdateCopy(java.lang.Object entity) throws DataAccessException
HibernateOperations
saveOrUpdateCopy
in interface HibernateOperations
entity
- the persistent object to save respectively update
(not necessarily to be associated with the Hibernate Session)
DataAccessException
- in case of Hibernate errorsSession.saveOrUpdateCopy(Object)
public void update(java.lang.Object entity) throws DataAccessException
HibernateOperations
update
in interface HibernateOperations
entity
- the persistent instance to update
DataAccessException
- in case of Hibernate errorsSession.update(Object)
public void update(java.lang.Object entity, net.sf.hibernate.LockMode lockMode) throws DataAccessException
HibernateOperations
update
in interface HibernateOperations
entity
- the persistent instance to update
DataAccessException
- in case of Hibernate errorsHibernateOptimisticLockingFailureException
,
Session.update(Object)
public void delete(java.lang.Object entity) throws DataAccessException
HibernateOperations
delete
in interface HibernateOperations
entity
- the persistent instance to delete
DataAccessException
- in case of Hibernate errorsSession.delete(Object)
public void delete(java.lang.Object entity, net.sf.hibernate.LockMode lockMode) throws DataAccessException
HibernateOperations
delete
in interface HibernateOperations
entity
- the persistent instance to delete
DataAccessException
- in case of Hibernate errorsHibernateOptimisticLockingFailureException
,
Session.delete(Object)
public void deleteAll(java.util.Collection entities) throws DataAccessException
HibernateOperations
deleteAll
in interface HibernateOperations
entities
- the persistent instances to delete
DataAccessException
- in case of Hibernate errorsSession.delete(String)
public java.util.List find(java.lang.String queryString) throws DataAccessException
HibernateOperations
find
in interface HibernateOperations
queryString
- a query expressed in Hibernate's query language
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.createQuery(java.lang.String)
public java.util.List find(java.lang.String queryString, java.lang.Object value) throws DataAccessException
HibernateOperations
find
in interface HibernateOperations
queryString
- a query expressed in Hibernate's query languagevalue
- the value of the parameter
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.createQuery(java.lang.String)
public java.util.List find(java.lang.String queryString, java.lang.Object value, net.sf.hibernate.type.Type type) throws DataAccessException
HibernateOperations
find
in interface HibernateOperations
queryString
- a query expressed in Hibernate's query languagevalue
- the value of the parametertype
- Hibernate type of the parameter
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.createQuery(java.lang.String)
public java.util.List find(java.lang.String queryString, java.lang.Object[] values) throws DataAccessException
HibernateOperations
find
in interface HibernateOperations
queryString
- a query expressed in Hibernate's query languagevalues
- the values of the parameters
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.createQuery(java.lang.String)
public java.util.List find(java.lang.String queryString, java.lang.Object[] values, net.sf.hibernate.type.Type[] types) throws DataAccessException
HibernateOperations
find
in interface HibernateOperations
queryString
- a query expressed in Hibernate's query languagevalues
- the values of the parameterstypes
- Hibernate types of the parameters
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.createQuery(java.lang.String)
public java.util.List findByValueBean(java.lang.String queryString, java.lang.Object valueBean) throws DataAccessException
HibernateOperations
findByValueBean
in interface HibernateOperations
queryString
- a query expressed in Hibernate's query languagevalueBean
- the values of the parameters
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.createQuery(java.lang.String)
,
Query.setProperties(java.lang.Object)
public java.util.List findByNamedQuery(java.lang.String queryName) throws DataAccessException
HibernateOperations
findByNamedQuery
in interface HibernateOperations
queryName
- the name of a Hibernate query in a mapping file
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.getNamedQuery(String)
public java.util.List findByNamedQuery(java.lang.String queryName, java.lang.Object value) throws DataAccessException
HibernateOperations
findByNamedQuery
in interface HibernateOperations
queryName
- the name of a Hibernate query in a mapping file
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.getNamedQuery(String)
public java.util.List findByNamedQuery(java.lang.String queryName, java.lang.Object value, net.sf.hibernate.type.Type type) throws DataAccessException
HibernateOperations
findByNamedQuery
in interface HibernateOperations
queryName
- the name of a Hibernate query in a mapping filetype
- Hibernate type of the parameter
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.getNamedQuery(String)
public java.util.List findByNamedQuery(java.lang.String queryName, java.lang.Object[] values) throws DataAccessException
HibernateOperations
findByNamedQuery
in interface HibernateOperations
queryName
- the name of a Hibernate query in a mapping filevalues
- the values of the parameters
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.getNamedQuery(String)
public java.util.List findByNamedQuery(java.lang.String queryName, java.lang.Object[] values, net.sf.hibernate.type.Type[] types) throws DataAccessException
HibernateOperations
findByNamedQuery
in interface HibernateOperations
queryName
- the name of a Hibernate query in a mapping filevalues
- the values of the parameterstypes
- Hibernate types of the parameters
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.getNamedQuery(String)
public java.util.List findByNamedQueryAndValueBean(java.lang.String queryName, java.lang.Object valueBean) throws DataAccessException
HibernateOperations
findByNamedQueryAndValueBean
in interface HibernateOperations
queryName
- the name of a Hibernate query in a mapping filevalueBean
- the values of the parameters
DataAccessException
- in case of Hibernate errorsSession.find(String)
,
Session.getNamedQuery(String)
,
Query.setProperties(java.lang.Object)
public net.sf.hibernate.Query createQuery(net.sf.hibernate.Session session, java.lang.String queryString) throws net.sf.hibernate.HibernateException
Applies a transaction timeout, if any. If you don't use such timeouts, the call is equivalent to Session.createQuery.
session
- current Hibernate SessionqueryString
- the HQL query string
net.sf.hibernate.HibernateException
- if the Query could not be createdHibernateCallback.doInHibernate(net.sf.hibernate.Session)
,
Session.createQuery(java.lang.String)
public net.sf.hibernate.Query getNamedQuery(net.sf.hibernate.Session session, java.lang.String queryName) throws net.sf.hibernate.HibernateException
Applies a transaction timeout, if any. If you don't use such timeouts, the call is equivalent to Session.getNamedQuery.
session
- current Hibernate SessionqueryName
- the name of the query in the Hibernate mapping file
net.sf.hibernate.HibernateException
- if the Query could not be createdHibernateCallback.doInHibernate(net.sf.hibernate.Session)
,
Session.getNamedQuery(java.lang.String)
public net.sf.hibernate.Criteria createCriteria(net.sf.hibernate.Session session, java.lang.Class entityClass) throws net.sf.hibernate.HibernateException
Applies a transaction timeout, if any. If you don't use such timeouts, the call is equivalent to Session.createCriteria.
session
- current Hibernate SessionentityClass
- the entity class to create the Criteria for
net.sf.hibernate.HibernateException
- if the Criteria could not be createdHibernateCallback.doInHibernate(net.sf.hibernate.Session)
,
Session.createCriteria(java.lang.Class)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |