Class GrailsHibernateTemplate

java.lang.Object
org.grails.orm.hibernate.GrailsHibernateTemplate
All Implemented Interfaces:
IHibernateTemplate

public class GrailsHibernateTemplate extends Object implements IHibernateTemplate
  • Field Details

    • exposeNativeSession

      protected boolean exposeNativeSession
    • cacheQueries

      protected boolean cacheQueries
    • sessionFactory

      protected org.hibernate.SessionFactory sessionFactory
    • dataSource

      protected DataSource dataSource
    • jdbcExceptionTranslator

      protected org.springframework.jdbc.support.SQLExceptionTranslator jdbcExceptionTranslator
    • flushMode

      protected int flushMode
    • FLUSH_NEVER

      public static final int FLUSH_NEVER
      Never flush is a good strategy for read-only units of work. Hibernate will not track and look for changes in this case, avoiding any overhead of modification detection.

      In case of an existing Session, FLUSH_NEVER will turn the flush mode to NEVER for the scope of the current operation, resetting the previous flush mode afterwards.

      See Also:
    • FLUSH_AUTO

      public static final int FLUSH_AUTO
      Automatic flushing is the default mode for a Hibernate Session. A session will get flushed on transaction commit, and on certain find operations that might involve already modified instances, but not after each unit of work like with eager flushing.

      In case of an existing Session, FLUSH_AUTO will participate in the existing flush mode, not modifying it for the current operation. This in particular means that this setting will not modify an existing flush mode NEVER, in contrast to FLUSH_EAGER.

      See Also:
    • FLUSH_EAGER

      public static final int FLUSH_EAGER
      Eager flushing leads to immediate synchronization with the database, even if in a transaction. This causes inconsistencies to show up and throw a respective exception immediately, and JDBC access code that participates in the same transaction will see the changes as the database is already aware of them then. But the drawbacks are:
      • additional communication roundtrips with the database, instead of a single batch at transaction commit;
      • the fact that an actual database rollback is needed if the Hibernate transaction rolls back (due to already submitted SQL statements).

      In case of an existing Session, FLUSH_EAGER will turn the flush mode to AUTO for the scope of the current operation and issue a flush at the end, resetting the previous flush mode afterwards.

      See Also:
    • FLUSH_COMMIT

      public static final int FLUSH_COMMIT
      Flushing at commit only is intended for units of work where no intermediate flushing is desired, not even for find operations that might involve already modified instances.

      In case of an existing Session, FLUSH_COMMIT will turn the flush mode to COMMIT for the scope of the current operation, resetting the previous flush mode afterwards. The only exception is an existing flush mode NEVER, which will not be modified through this setting.

      See Also:
    • FLUSH_ALWAYS

      public static final int FLUSH_ALWAYS
      Flushing before every query statement is rarely necessary. It is only available for special needs.

      In case of an existing Session, FLUSH_ALWAYS will turn the flush mode to ALWAYS for the scope of the current operation, resetting the previous flush mode afterwards.

      See Also:
  • Constructor Details

    • GrailsHibernateTemplate

      protected GrailsHibernateTemplate()
    • GrailsHibernateTemplate

      public GrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory)
    • GrailsHibernateTemplate

      public GrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory, HibernateDatastore datastore)
    • GrailsHibernateTemplate

      public GrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory, HibernateDatastore datastore, int defaultFlushMode)
  • Method Details

    • execute

      public <T> T execute(groovy.lang.Closure<T> callable)
      Specified by:
      execute in interface IHibernateTemplate
    • executeWithNewSession

      public <T> T executeWithNewSession(groovy.lang.Closure<T> callable)
      Specified by:
      executeWithNewSession in interface IHibernateTemplate
    • executeWithExistingOrCreateNewSession

      public <T1> T1 executeWithExistingOrCreateNewSession(org.hibernate.SessionFactory sessionFactory, groovy.lang.Closure<T1> callable)
      Specified by:
      executeWithExistingOrCreateNewSession in interface IHibernateTemplate
    • getSessionFactory

      public org.hibernate.SessionFactory getSessionFactory()
      Specified by:
      getSessionFactory in interface IHibernateTemplate
    • applySettings

      public void applySettings(org.hibernate.query.Query query)
      Specified by:
      applySettings in interface IHibernateTemplate
    • applySettings

      public void applySettings(org.hibernate.Criteria criteria)
      Specified by:
      applySettings in interface IHibernateTemplate
    • setCacheQueries

      public void setCacheQueries(boolean cacheQueries)
    • isCacheQueries

      public boolean isCacheQueries()
    • execute

      public <T> T execute(GrailsHibernateTemplate.HibernateCallback<T> action) throws org.springframework.dao.DataAccessException
      Throws:
      org.springframework.dao.DataAccessException
    • executeFind

      public List<?> executeFind(GrailsHibernateTemplate.HibernateCallback<?> action) throws org.springframework.dao.DataAccessException
      Throws:
      org.springframework.dao.DataAccessException
    • shouldPassReadOnlyToHibernate

      protected boolean shouldPassReadOnlyToHibernate()
    • isOsivReadOnly

      public boolean isOsivReadOnly()
    • setOsivReadOnly

      public void setOsivReadOnly(boolean osivReadOnly)
    • doExecute

      protected <T> T doExecute(GrailsHibernateTemplate.HibernateCallback<T> action, boolean enforceNativeSession) throws org.springframework.dao.DataAccessException
      Execute the action specified by the given action object within a Session.
      Parameters:
      action - callback object that specifies the Hibernate action
      enforceNativeSession - whether to enforce exposure of the native Hibernate Session to callback code
      Returns:
      a result object returned by the action, or null
      Throws:
      org.springframework.dao.DataAccessException - in case of Hibernate errors
    • isSessionTransactional

      protected boolean isSessionTransactional(org.hibernate.Session session)
    • getSession

      protected org.hibernate.Session getSession()
    • createSessionProxy

      protected org.hibernate.Session createSessionProxy(org.hibernate.Session session)
      Create a close-suppressing proxy for the given Hibernate Session. The proxy also prepares returned Query and Criteria objects.
      Parameters:
      session - the Hibernate Session to create a proxy for
      Returns:
      the Session proxy
      See Also:
    • get

      public <T> T get(Class<T> entityClass, Serializable id) throws org.springframework.dao.DataAccessException
      Specified by:
      get in interface IHibernateTemplate
      Throws:
      org.springframework.dao.DataAccessException
    • get

      public <T> T get(Class<T> entityClass, Serializable id, org.hibernate.LockMode mode)
      Specified by:
      get in interface IHibernateTemplate
    • delete

      public void delete(Object entity) throws org.springframework.dao.DataAccessException
      Specified by:
      delete in interface IHibernateTemplate
      Throws:
      org.springframework.dao.DataAccessException
    • flush

      public void flush(Object entity) throws org.springframework.dao.DataAccessException
      Throws:
      org.springframework.dao.DataAccessException
    • load

      public <T> T load(Class<T> entityClass, Serializable id) throws org.springframework.dao.DataAccessException
      Specified by:
      load in interface IHibernateTemplate
      Throws:
      org.springframework.dao.DataAccessException
    • lock

      public <T> T lock(Class<T> entityClass, Serializable id, org.hibernate.LockMode lockMode) throws org.springframework.dao.DataAccessException
      Throws:
      org.springframework.dao.DataAccessException
    • loadAll

      public <T> List<T> loadAll(Class<T> entityClass) throws org.springframework.dao.DataAccessException
      Throws:
      org.springframework.dao.DataAccessException
    • contains

      public boolean contains(Object entity) throws org.springframework.dao.DataAccessException
      Specified by:
      contains in interface IHibernateTemplate
      Throws:
      org.springframework.dao.DataAccessException
    • evict

      public void evict(Object entity) throws org.springframework.dao.DataAccessException
      Specified by:
      evict in interface IHibernateTemplate
      Throws:
      org.springframework.dao.DataAccessException
    • lock

      public void lock(Object entity, org.hibernate.LockMode lockMode) throws org.springframework.dao.DataAccessException
      Specified by:
      lock in interface IHibernateTemplate
      Throws:
      org.springframework.dao.DataAccessException
    • refresh

      public void refresh(Object entity) throws org.springframework.dao.DataAccessException
      Specified by:
      refresh in interface IHibernateTemplate
      Throws:
      org.springframework.dao.DataAccessException
    • refresh

      public void refresh(Object entity, org.hibernate.LockMode lockMode) throws org.springframework.dao.DataAccessException
      Throws:
      org.springframework.dao.DataAccessException
    • setExposeNativeSession

      public void setExposeNativeSession(boolean exposeNativeSession)
    • isExposeNativeSession

      public boolean isExposeNativeSession()
    • prepareQuery

      protected void prepareQuery(org.hibernate.query.Query query)
      Prepare the given Query object, applying cache settings and/or a transaction timeout.
      Parameters:
      query - the Query object to prepare
    • prepareCriteria

      @Deprecated protected void prepareCriteria(org.hibernate.Criteria criteria)
      Deprecated.
      Deprecated because Hibernate Criteria are deprecated
      Prepare the given Criteria object, applying cache settings and/or a transaction timeout.
      Parameters:
      criteria - the Criteria object to prepare
    • prepareCriteria

      protected <T> void prepareCriteria(org.hibernate.query.Query<T> jpaQuery)
      Prepare the given Query object, applying cache settings and/or a transaction timeout.
      Parameters:
      jpaQuery - the Query object to prepare
    • setFlushMode

      public void setFlushMode(int flushMode)
      Set the flush behavior to one of the constants in this class. Default is FLUSH_AUTO.
      Specified by:
      setFlushMode in interface IHibernateTemplate
      See Also:
    • getFlushMode

      public int getFlushMode()
      Return if a flush should be forced after executing the callback code.
      Specified by:
      getFlushMode in interface IHibernateTemplate
    • applyFlushMode

      protected org.hibernate.FlushMode applyFlushMode(org.hibernate.Session session, boolean existingTransaction)
      Apply the flush mode that's been specified for this accessor to the given Session.
      Parameters:
      session - the current Hibernate Session
      existingTransaction - if executing within an existing transaction
      Returns:
      the previous flush mode to restore after the operation, or null if none
      See Also:
    • flushIfNecessary

      protected void flushIfNecessary(org.hibernate.Session session, boolean existingTransaction) throws org.hibernate.HibernateException
      Throws:
      org.hibernate.HibernateException
    • convertHibernateAccessException

      protected org.springframework.dao.DataAccessException convertHibernateAccessException(org.hibernate.HibernateException ex)
    • convertJdbcAccessException

      protected org.springframework.dao.DataAccessException convertJdbcAccessException(org.hibernate.JDBCException ex, org.springframework.jdbc.support.SQLExceptionTranslator translator)
    • save

      public Serializable save(Object o)
      Specified by:
      save in interface IHibernateTemplate
    • flush

      public void flush()
      Specified by:
      flush in interface IHibernateTemplate
    • clear

      public void clear()
      Specified by:
      clear in interface IHibernateTemplate
    • deleteAll

      public void deleteAll(Collection<?> objects)
      Specified by:
      deleteAll in interface IHibernateTemplate
    • getIterableAsCollection

      protected Collection getIterableAsCollection(Iterable objects)
    • isApplyFlushModeOnlyToNonExistingTransactions

      public boolean isApplyFlushModeOnlyToNonExistingTransactions()
    • setApplyFlushModeOnlyToNonExistingTransactions

      public void setApplyFlushModeOnlyToNonExistingTransactions(boolean applyFlushModeOnlyToNonExistingTransactions)