Class GrailsHibernateTemplate

    • Field Detail

      • exposeNativeSession

        protected boolean exposeNativeSession
      • cacheQueries

        protected boolean cacheQueries
      • sessionFactory

        protected org.hibernate.SessionFactory sessionFactory
      • dataSource

        protected javax.sql.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:
        setFlushMode(int), Constant Field Values
      • 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:
        setFlushMode(int), Constant Field Values
      • 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:
        setFlushMode(int), Constant Field Values
      • 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:
        setFlushMode(int), Constant Field Values
      • 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:
        setFlushMode(int), Constant Field Values
    • Constructor Detail

      • 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 Detail

      • execute

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

        public void setCacheQueries​(boolean cacheQueries)
      • isCacheQueries

        public boolean isCacheQueries()
      • executeFind

        public java.util.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:
        SharedSessionContract.close(), prepareQuery(org.hibernate.query.Query), prepareCriteria(org.hibernate.Criteria)
      • get

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

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

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

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

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

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

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

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

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

        public void lock​(java.lang.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​(java.lang.Object entity)
                     throws org.springframework.dao.DataAccessException
        Specified by:
        refresh in interface IHibernateTemplate
        Throws:
        org.springframework.dao.DataAccessException
      • refresh

        public void refresh​(java.lang.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:
        FLUSH_AUTO
      • 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:
        setFlushMode(int), Session.setFlushMode(org.hibernate.FlushMode)
      • 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 java.io.Serializable save​(java.lang.Object o)
        Specified by:
        save in interface IHibernateTemplate
      • getIterableAsCollection

        protected java.util.Collection getIterableAsCollection​(java.lang.Iterable objects)
      • isApplyFlushModeOnlyToNonExistingTransactions

        public boolean isApplyFlushModeOnlyToNonExistingTransactions()
      • setApplyFlushModeOnlyToNonExistingTransactions

        public void setApplyFlushModeOnlyToNonExistingTransactions​(boolean applyFlushModeOnlyToNonExistingTransactions)