org.springframework.orm.hibernate5

Class HibernateTemplate

  • java.lang.Object
    • org.springframework.orm.hibernate5.HibernateTemplate
  • All Implemented Interfaces:
    InitializingBean, HibernateOperations


    public class HibernateTemplate
    extends java.lang.Object
    implements HibernateOperations, InitializingBean
    Helper class that simplifies Hibernate data access code. Automatically converts HibernateExceptions into DataAccessExceptions, following the org.springframework.dao exception hierarchy.

    The central method is execute, supporting Hibernate access 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.

    NOTE: Hibernate access code can also be coded against the native Hibernate Session. Hence, for newly started projects, consider adopting the standard Hibernate style of coding against SessionFactory.getCurrentSession(). Alternatively, use execute(HibernateCallback) with Java 8 lambda code blocks against the callback-provided Session which results in elegant code as well, decoupled from the Hibernate Session lifecycle. The remaining operations on this HibernateTemplate are deprecated in the meantime and primarily exist as a migration helper for older Hibernate 3.x/4.x data access code in existing applications.

    Since:
    4.2
    Author:
    Juergen Hoeller
    See Also:
    setSessionFactory(org.hibernate.SessionFactory), HibernateCallback, Session, LocalSessionFactoryBean, HibernateTransactionManager, OpenSessionInViewFilter, OpenSessionInViewInterceptor
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      protected Log logger 
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method and Description
      void afterPropertiesSet()
      Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.
      protected void applyNamedParameterToQuery(Query queryObject, java.lang.String paramName, java.lang.Object value)
      Deprecated. 
      int bulkUpdate(java.lang.String queryString, java.lang.Object... values)
      Deprecated. 
      protected void checkWriteOperationAllowed(Session session)
      Check whether write operations are allowed on the given Session.
      void clear()
      Remove all objects from the Session cache, and cancel all pending saves, updates and deletes.
      void closeIterator(java.util.Iterator<?> it)
      Deprecated. 
      boolean contains(java.lang.Object entity)
      Check whether the given object is in the Session cache.
      protected Session createSessionProxy(Session session)
      Create a close-suppressing proxy for the given Hibernate Session.
      void delete(java.lang.Object entity)
      Delete the given persistent instance.
      void delete(java.lang.Object entity, LockMode lockMode)
      Delete the given persistent instance.
      void delete(java.lang.String entityName, java.lang.Object entity)
      Delete the given persistent instance.
      void delete(java.lang.String entityName, java.lang.Object entity, LockMode lockMode)
      Delete the given persistent instance.
      void deleteAll(java.util.Collection<?> entities)
      Delete all given persistent instances.
      protected void disableFilters(Session session)
      Disable the specified filters on the given Session.
      protected <T> T doExecute(HibernateCallback<T> action, boolean enforceNativeSession)
      Execute the action specified by the given action object within a Session.
      Filter enableFilter(java.lang.String filterName)
      Return an enabled Hibernate Filter for the given filter name.
      protected void enableFilters(Session session)
      Enable the specified filters on the given Session.
      void evict(java.lang.Object entity)
      Remove the given object from the Session cache.
      <T> T execute(HibernateCallback<T> action)
      Execute the action specified by the given action object within a Session.
      <T> T executeWithNativeSession(HibernateCallback<T> action)
      Execute the action specified by the given action object within a native Session.
      java.util.List<?> find(java.lang.String queryString, java.lang.Object... values)
      Deprecated. 
      java.util.List<?> findByCriteria(DetachedCriteria criteria)
      Execute a query based on a given Hibernate criteria object.
      java.util.List<?> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults)
      Execute a query based on the given Hibernate criteria object.
      <T> java.util.List<T> findByExample(java.lang.String entityName, T exampleEntity)
      Execute a query based on the given example entity object.
      <T> java.util.List<T> findByExample(java.lang.String entityName, T exampleEntity, int firstResult, int maxResults)
      Execute a query based on a given example entity object.
      <T> java.util.List<T> findByExample(T exampleEntity)
      Execute a query based on the given example entity object.
      <T> java.util.List<T> findByExample(T exampleEntity, int firstResult, int maxResults)
      Execute a query based on a given example entity object.
      java.util.List<?> findByNamedParam(java.lang.String queryString, java.lang.String[] paramNames, java.lang.Object[] values)
      Deprecated. 
      java.util.List<?> findByNamedParam(java.lang.String queryString, java.lang.String paramName, java.lang.Object value)
      Deprecated. 
      java.util.List<?> findByNamedQuery(java.lang.String queryName, java.lang.Object... values)
      Deprecated. 
      java.util.List<?> findByNamedQueryAndNamedParam(java.lang.String queryName, java.lang.String[] paramNames, java.lang.Object[] values)
      Deprecated. 
      java.util.List<?> findByNamedQueryAndNamedParam(java.lang.String queryName, java.lang.String paramName, java.lang.Object value)
      Deprecated. 
      java.util.List<?> findByNamedQueryAndValueBean(java.lang.String queryName, java.lang.Object valueBean)
      Deprecated. 
      java.util.List<?> findByValueBean(java.lang.String queryString, java.lang.Object valueBean)
      Deprecated. 
      void flush()
      Flush all pending saves, updates and deletes to the database.
      <T> T get(java.lang.Class<T> entityClass, java.io.Serializable id)
      Return the persistent instance of the given entity class with the given identifier, or null if not found.
      <T> T get(java.lang.Class<T> entityClass, java.io.Serializable id, LockMode lockMode)
      Return the persistent instance of the given entity class with the given identifier, or null if not found.
      java.lang.Object get(java.lang.String entityName, 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.String entityName, java.io.Serializable id, LockMode lockMode)
      Return the persistent instance of the given entity class with the given identifier, or null if not found.
      int getFetchSize()
      Return the fetch size specified for this HibernateTemplate.
      java.lang.String[] getFilterNames()
      Return the names of Hibernate filters to be activated, if any.
      int getMaxResults()
      Return the maximum number of rows specified for this HibernateTemplate.
      java.lang.String getQueryCacheRegion()
      Return the name of the cache region for queries executed by this template.
      SessionFactory getSessionFactory()
      Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
      void initialize(java.lang.Object proxy)
      Force initialization of a Hibernate proxy or persistent collection.
      boolean isCacheQueries()
      Return whether to cache all queries executed by this template.
      boolean isCheckWriteOperations()
      Return whether to check that the Hibernate Session is not in read-only mode in case of write operations (save/update/delete).
      boolean isExposeNativeSession()
      Return whether to expose the native Hibernate Session to HibernateCallback code, or rather a Session proxy.
      java.util.Iterator<?> iterate(java.lang.String queryString, java.lang.Object... values)
      Deprecated. 
      <T> T load(java.lang.Class<T> entityClass, java.io.Serializable id)
      Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.
      <T> T load(java.lang.Class<T> entityClass, java.io.Serializable id, LockMode lockMode)
      Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.
      void load(java.lang.Object entity, java.io.Serializable id)
      Load the persistent instance with the given identifier into the given object, throwing an exception if not found.
      java.lang.Object load(java.lang.String entityName, 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.String entityName, java.io.Serializable id, LockMode lockMode)
      Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.
      <T> java.util.List<T> loadAll(java.lang.Class<T> entityClass)
      Return all persistent instances of the given entity class.
      void lock(java.lang.Object entity, LockMode lockMode)
      Obtain the specified lock level upon the given object, implicitly checking whether the corresponding database entry still exists.
      void lock(java.lang.String entityName, java.lang.Object entity, LockMode lockMode)
      Obtain the specified lock level upon the given object, implicitly checking whether the corresponding database entry still exists.
      <T> T merge(java.lang.String entityName, T entity)
      Copy the state of the given object onto the persistent object with the same identifier.
      <T> T merge(T entity)
      Copy the state of the given object onto the persistent object with the same identifier.
      protected SessionFactory obtainSessionFactory()
      Obtain the SessionFactory for actual use.
      void persist(java.lang.Object entity)
      Persist the given transient instance.
      void persist(java.lang.String entityName, java.lang.Object entity)
      Persist the given transient instance.
      protected void prepareCriteria(Criteria criteria)
      Prepare the given Criteria object, applying cache settings and/or a transaction timeout.
      protected void prepareQuery(Query queryObject)
      Prepare the given Query object, applying cache settings and/or a transaction timeout.
      void refresh(java.lang.Object entity)
      Re-read the state of the given persistent instance.
      void refresh(java.lang.Object entity, LockMode lockMode)
      Re-read the state of the given persistent instance.
      void replicate(java.lang.Object entity, ReplicationMode replicationMode)
      Persist the state of the given detached instance according to the given replication mode, reusing the current identifier value.
      void replicate(java.lang.String entityName, java.lang.Object entity, ReplicationMode replicationMode)
      Persist the state of the given detached instance according to the given replication mode, reusing the current identifier value.
      java.io.Serializable save(java.lang.Object entity)
      Persist the given transient instance.
      java.io.Serializable save(java.lang.String entityName, java.lang.Object entity)
      Persist the given transient instance.
      void saveOrUpdate(java.lang.Object entity)
      Save or update the given persistent instance, according to its id (matching the configured "unsaved-value"?).
      void saveOrUpdate(java.lang.String entityName, java.lang.Object entity)
      Save or update the given persistent instance, according to its id (matching the configured "unsaved-value"?).
      void setCacheQueries(boolean cacheQueries)
      Set whether to cache all queries executed by this template.
      void setCheckWriteOperations(boolean checkWriteOperations)
      Set whether to check that the Hibernate Session is not in read-only mode in case of write operations (save/update/delete).
      void setExposeNativeSession(boolean exposeNativeSession)
      Set whether to expose the native Hibernate Session to HibernateCallback code.
      void setFetchSize(int fetchSize)
      Set the fetch size for this HibernateTemplate.
      void setFilterNames(java.lang.String... filterNames)
      Set one or more names of Hibernate filters to be activated for all Sessions that this accessor works with.
      void setMaxResults(int maxResults)
      Set the maximum number of rows for this HibernateTemplate.
      void setQueryCacheRegion(java.lang.String queryCacheRegion)
      Set the name of the cache region for queries executed by this template.
      void setSessionFactory(SessionFactory sessionFactory)
      Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
      void update(java.lang.Object entity)
      Update the given persistent instance, associating it with the current Hibernate Session.
      void update(java.lang.Object entity, LockMode lockMode)
      Update the given persistent instance, associating it with the current Hibernate Session.
      void update(java.lang.String entityName, java.lang.Object entity)
      Update the given persistent instance, associating it with the current Hibernate Session.
      void update(java.lang.String entityName, java.lang.Object entity, LockMode lockMode)
      Update the given persistent instance, associating it with the current Hibernate Session.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait