类 HibernateTemplate

  • 所有已实现的接口:
    InitializingBean, HibernateOperations

    public class HibernateTemplate
    extends 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 in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate style of coding data access objects instead, based on SessionFactory.getCurrentSession(). This HibernateTemplate primarily exists as a migration helper for Hibernate 3 based data access code, to benefit from bug fixes in Hibernate 4.x.

    从以下版本开始:
    4.0.1
    作者:
    Juergen Hoeller
    另请参阅:
    setSessionFactory(org.hibernate.SessionFactory), HibernateCallback, Session, LocalSessionFactoryBean, HibernateTransactionManager, OpenSessionInViewFilter, OpenSessionInViewInterceptor
    • 字段概要

      字段 
      修饰符和类型字段说明
      protected Loglogger 
    • 方法概要

      所有方法 实例方法 具体方法 
      修饰符和类型方法说明
      voidafterPropertiesSet()
      Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.
      protected voidapplyNamedParameterToQuery​(Query queryObject, String paramName, Object value)
      Apply the given name parameter to the given Query object.
      intbulkUpdate​(String queryString, Object... values)
      Update/delete all objects according to the given query, binding a number of values to "?"
      protected voidcheckWriteOperationAllowed​(Session session)
      Check whether write operations are allowed on the given Session.
      voidclear()
      Remove all objects from the Session cache, and cancel all pending saves, updates and deletes.
      voidcloseIterator​(Iterator<?> it)
      Immediately close an Iterator created by any of the various iterate(..) operations, instead of waiting until the session is closed or disconnected.
      booleancontains​(Object entity)
      Check whether the given object is in the Session cache.
      protected SessioncreateSessionProxy​(Session session)
      Create a close-suppressing proxy for the given Hibernate Session.
      voiddelete​(Object entity)
      Delete the given persistent instance.
      voiddelete​(Object entity, LockMode lockMode)
      Delete the given persistent instance.
      voiddelete​(String entityName, Object entity)
      Delete the given persistent instance.
      voiddelete​(String entityName, Object entity, LockMode lockMode)
      Delete the given persistent instance.
      voiddeleteAll​(Collection<?> entities)
      Delete all given persistent instances.
      protected voiddisableFilters​(Session session)
      Disable the specified filters on the given Session.
      protected <T> TdoExecute​(HibernateCallback<T> action, boolean enforceNativeSession)
      Execute the action specified by the given action object within a Session.
      FilterenableFilter​(String filterName)
      Return an enabled Hibernate Filter for the given filter name.
      protected voidenableFilters​(Session session)
      Enable the specified filters on the given Session.
      voidevict​(Object entity)
      Remove the given object from the Session cache.
      <T> Texecute​(HibernateCallback<T> action)
      Execute the action specified by the given action object within a Session.
      <T> TexecuteWithNativeSession​(HibernateCallback<T> action)
      Execute the action specified by the given action object within a native Session.
      List<?>find​(String queryString, Object... values)
      Execute an HQL query, binding a number of values to "?"
      List<?>findByCriteria​(DetachedCriteria criteria)
      Execute a query based on a given Hibernate criteria object.
      List<?>findByCriteria​(DetachedCriteria criteria, int firstResult, int maxResults)
      Execute a query based on the given Hibernate criteria object.
      <T> List<T>findByExample​(String entityName, T exampleEntity)
      Execute a query based on the given example entity object.
      <T> List<T>findByExample​(String entityName, T exampleEntity, int firstResult, int maxResults)
      Execute a query based on a given example entity object.
      <T> List<T>findByExample​(T exampleEntity)
      Execute a query based on the given example entity object.
      <T> List<T>findByExample​(T exampleEntity, int firstResult, int maxResults)
      Execute a query based on a given example entity object.
      List<?>findByNamedParam​(String queryString, String[] paramNames, Object[] values)
      Execute an HQL query, binding a number of values to ":" named parameters in the query string.
      List<?>findByNamedParam​(String queryString, String paramName, Object value)
      Execute an HQL query, binding one value to a ":" named parameter in the query string.
      List<?>findByNamedQuery​(String queryName, Object... values)
      Execute a named query binding a number of values to "?"
      List<?>findByNamedQueryAndNamedParam​(String queryName, String[] paramNames, Object[] values)
      Execute a named query, binding a number of values to ":" named parameters in the query string.
      List<?>findByNamedQueryAndNamedParam​(String queryName, String paramName, Object value)
      Execute a named query, binding one value to a ":" named parameter in the query string.
      List<?>findByNamedQueryAndValueBean​(String queryName, Object valueBean)
      Execute a named query, binding the properties of the given bean to ":" named parameters in the query string.
      List<?>findByValueBean​(String queryString, Object valueBean)
      Execute an HQL query, binding the properties of the given bean to named parameters in the query string.
      voidflush()
      Flush all pending saves, updates and deletes to the database.
      <T> Tget​(Class<T> entityClass, Serializable id)
      Return the persistent instance of the given entity class with the given identifier, or null if not found.
      <T> Tget​(Class<T> entityClass, Serializable id, LockMode lockMode)
      Return the persistent instance of the given entity class with the given identifier, or null if not found.
      Objectget​(String entityName, Serializable id)
      Return the persistent instance of the given entity class with the given identifier, or null if not found.
      Objectget​(String entityName, Serializable id, LockMode lockMode)
      Return the persistent instance of the given entity class with the given identifier, or null if not found.
      intgetFetchSize()
      Return the fetch size specified for this HibernateTemplate.
      String[]getFilterNames()
      Return the names of Hibernate filters to be activated, if any.
      intgetMaxResults()
      Return the maximum number of rows specified for this HibernateTemplate.
      StringgetQueryCacheRegion()
      Return the name of the cache region for queries executed by this template.
      SessionFactorygetSessionFactory()
      Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
      voidinitialize​(Object proxy)
      Force initialization of a Hibernate proxy or persistent collection.
      booleanisCacheQueries()
      Return whether to cache all queries executed by this template.
      booleanisCheckWriteOperations()
      Return whether to check that the Hibernate Session is not in read-only mode in case of write operations (save/update/delete).
      booleanisExposeNativeSession()
      Return whether to expose the native Hibernate Session to HibernateCallback code, or rather a Session proxy.
      Iterator<?>iterate​(String queryString, Object... values)
      Execute a query for persistent instances, binding a number of values to "?"
      <T> Tload​(Class<T> entityClass, Serializable id)
      Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.
      <T> Tload​(Class<T> entityClass, Serializable id, LockMode lockMode)
      Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.
      voidload​(Object entity, Serializable id)
      Load the persistent instance with the given identifier into the given object, throwing an exception if not found.
      Objectload​(String entityName, Serializable id)
      Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.
      Objectload​(String entityName, Serializable id, LockMode lockMode)
      Return the persistent instance of the given entity class with the given identifier, throwing an exception if not found.
      <T> List<T>loadAll​(Class<T> entityClass)
      Return all persistent instances of the given entity class.
      voidlock​(Object entity, LockMode lockMode)
      Obtain the specified lock level upon the given object, implicitly checking whether the corresponding database entry still exists.
      voidlock​(String entityName, Object entity, LockMode lockMode)
      Obtain the specified lock level upon the given object, implicitly checking whether the corresponding database entry still exists.
      <T> Tmerge​(String entityName, T entity)
      Copy the state of the given object onto the persistent object with the same identifier.
      <T> Tmerge​(T entity)
      Copy the state of the given object onto the persistent object with the same identifier.
      voidpersist​(Object entity)
      Persist the given transient instance.
      voidpersist​(String entityName, Object entity)
      Persist the given transient instance.
      protected voidprepareCriteria​(Criteria criteria)
      Prepare the given Criteria object, applying cache settings and/or a transaction timeout.
      protected voidprepareQuery​(Query queryObject)
      Prepare the given Query object, applying cache settings and/or a transaction timeout.
      voidrefresh​(Object entity)
      Re-read the state of the given persistent instance.
      voidrefresh​(Object entity, LockMode lockMode)
      Re-read the state of the given persistent instance.
      voidreplicate​(Object entity, ReplicationMode replicationMode)
      Persist the state of the given detached instance according to the given replication mode, reusing the current identifier value.
      voidreplicate​(String entityName, Object entity, ReplicationMode replicationMode)
      Persist the state of the given detached instance according to the given replication mode, reusing the current identifier value.
      Serializablesave​(Object entity)
      Persist the given transient instance.
      Serializablesave​(String entityName, Object entity)
      Persist the given transient instance.
      voidsaveOrUpdate​(Object entity)
      Save or update the given persistent instance, according to its id (matching the configured "unsaved-value"?).
      voidsaveOrUpdate​(String entityName, Object entity)
      Save or update the given persistent instance, according to its id (matching the configured "unsaved-value"?).
      voidsetCacheQueries​(boolean cacheQueries)
      Set whether to cache all queries executed by this template.
      voidsetCheckWriteOperations​(boolean checkWriteOperations)
      Set whether to check that the Hibernate Session is not in read-only mode in case of write operations (save/update/delete).
      voidsetExposeNativeSession​(boolean exposeNativeSession)
      Set whether to expose the native Hibernate Session to HibernateCallback code.
      voidsetFetchSize​(int fetchSize)
      Set the fetch size for this HibernateTemplate.
      voidsetFilterNames​(String... filterNames)
      Set one or more names of Hibernate filters to be activated for all Sessions that this accessor works with.
      voidsetMaxResults​(int maxResults)
      Set the maximum number of rows for this HibernateTemplate.
      voidsetQueryCacheRegion​(String queryCacheRegion)
      Set the name of the cache region for queries executed by this template.
      voidsetSessionFactory​(SessionFactory sessionFactory)
      Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
      voidupdate​(Object entity)
      Update the given persistent instance, associating it with the current Hibernate Session.
      voidupdate​(Object entity, LockMode lockMode)
      Update the given persistent instance, associating it with the current Hibernate Session.
      voidupdate​(String entityName, Object entity)
      Update the given persistent instance, associating it with the current Hibernate Session.
      voidupdate​(String entityName, Object entity, LockMode lockMode)
      Update the given persistent instance, associating it with the current Hibernate Session.