org.codehaus.groovy.grails.orm.hibernate
Class GrailsHibernateTemplate

java.lang.Object
  extended by org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate
All Implemented Interfaces:
org.codehaus.groovy.grails.orm.hibernate.IHibernateTemplate

public class GrailsHibernateTemplate
extends java.lang.Object
implements org.codehaus.groovy.grails.orm.hibernate.IHibernateTemplate


Nested Class Summary
protected  class GrailsHibernateTemplate.CloseSuppressingInvocationHandler
          Invocation handler that suppresses close calls on Hibernate Sessions.
static interface GrailsHibernateTemplate.HibernateCallback<T>
           
 
Field Summary
protected  boolean allowCreate
           
protected  boolean cacheQueries
           
protected  boolean checkWriteOperations
           
protected  boolean exposeNativeSession
           
static int FLUSH_ALWAYS
          Flushing before every query statement is rarely necessary.
static int FLUSH_AUTO
          Automatic flushing is the default mode for a Hibernate Session.
static 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.
static int FLUSH_EAGER
          Eager flushing leads to immediate synchronization with the database, even if in a transaction.
static int FLUSH_NEVER
          Never flush is a good strategy for read-only units of work.
protected  int flushMode
           
protected  org.springframework.jdbc.support.SQLExceptionTranslator jdbcExceptionTranslator
           
protected  org.hibernate.SessionFactory sessionFactory
           
 
Constructor Summary
protected GrailsHibernateTemplate()
           
  GrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory)
           
  GrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory, org.codehaus.groovy.grails.commons.GrailsApplication application)
           
 
Method Summary
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.
 void applySettings(org.hibernate.Criteria criteria)
           
 void applySettings(org.hibernate.Query query)
           
 void clear()
           
 boolean contains(java.lang.Object entity)
           
protected  org.springframework.dao.DataAccessException convertHibernateAccessException(org.hibernate.HibernateException ex)
           
protected  org.springframework.dao.DataAccessException convertJdbcAccessException(org.hibernate.JDBCException ex, org.springframework.jdbc.support.SQLExceptionTranslator translator)
           
protected  org.hibernate.Session createSessionProxy(org.hibernate.Session session)
          Create a close-suppressing proxy for the given Hibernate Session.
 void delete(java.lang.Object entity)
           
 void deleteAll(java.util.Collection<?> objects)
           
protected
<T> T
doExecute(GrailsHibernateTemplate.HibernateCallback<T> action, boolean enforceNativeSession)
          Execute the action specified by the given action object within a Session.
 void evict(java.lang.Object entity)
           
<T> T
execute(GrailsHibernateTemplate.HibernateCallback<T> action)
           
 java.util.List<?> executeFind(GrailsHibernateTemplate.HibernateCallback<?> action)
           
 void flush()
           
 void flush(java.lang.Object entity)
           
protected  void flushIfNecessary(org.hibernate.Session session, boolean existingTransaction)
           
<T> T
get(java.lang.Class<T> entityClass, java.io.Serializable id)
           
<T> T
get(java.lang.Class<T> entityClass, java.io.Serializable id, org.hibernate.LockMode mode)
           
 int getFlushMode()
          Return if a flush should be forced after executing the callback code.
protected  java.util.Collection getIterableAsCollection(java.lang.Iterable objects)
           
protected  org.hibernate.Session getSession()
           
 org.hibernate.SessionFactory getSessionFactory()
           
 boolean isCacheQueries()
           
protected  boolean isCurrentTransactionReadOnly()
           
 boolean isExposeNativeSession()
           
 boolean isOsivReadOnly()
           
protected  boolean isSessionTransactional(org.hibernate.Session session)
           
<T> T
load(java.lang.Class<T> entityClass, java.io.Serializable id)
           
<T> java.util.List<T>
loadAll(java.lang.Class<T> entityClass)
           
<T> T
lock(java.lang.Class<T> entityClass, java.io.Serializable id, org.hibernate.LockMode lockMode)
           
 void lock(java.lang.Object entity, org.hibernate.LockMode lockMode)
           
protected  void prepareCriteria(org.hibernate.Criteria criteria)
          Prepare the given Criteria object, applying cache settings and/or a transaction timeout.
protected  void prepareQuery(org.hibernate.Query query)
          Prepare the given Query object, applying cache settings and/or a transaction timeout.
 void refresh(java.lang.Object entity)
           
 void refresh(java.lang.Object entity, org.hibernate.LockMode lockMode)
           
 java.io.Serializable save(java.lang.Object o)
           
 void setCacheQueries(boolean cacheQueries)
           
 void setExposeNativeSession(boolean exposeNativeSession)
           
 void setFlushMode(int flushMode)
          Set the flush behavior to one of the constants in this class.
 void setOsivReadOnly(boolean osivReadOnly)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

exposeNativeSession

protected boolean exposeNativeSession

cacheQueries

protected boolean cacheQueries

allowCreate

protected boolean allowCreate

checkWriteOperations

protected boolean checkWriteOperations

sessionFactory

protected org.hibernate.SessionFactory sessionFactory

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:

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,
                               org.codehaus.groovy.grails.commons.GrailsApplication application)
Method Detail

getSessionFactory

public org.hibernate.SessionFactory getSessionFactory()
Specified by:
getSessionFactory in interface org.codehaus.groovy.grails.orm.hibernate.IHibernateTemplate

applySettings

public void applySettings(org.hibernate.Query query)

applySettings

public void applySettings(org.hibernate.Criteria criteria)

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 java.util.List<?> executeFind(GrailsHibernateTemplate.HibernateCallback<?> action)
                              throws org.springframework.dao.DataAccessException
Throws:
org.springframework.dao.DataAccessException

isCurrentTransactionReadOnly

protected boolean isCurrentTransactionReadOnly()

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:
Session.close(), prepareQuery(org.hibernate.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 org.codehaus.groovy.grails.orm.hibernate.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 org.codehaus.groovy.grails.orm.hibernate.IHibernateTemplate

delete

public void delete(java.lang.Object entity)
            throws org.springframework.dao.DataAccessException
Specified by:
delete in interface org.codehaus.groovy.grails.orm.hibernate.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 org.codehaus.groovy.grails.orm.hibernate.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 org.codehaus.groovy.grails.orm.hibernate.IHibernateTemplate
Throws:
org.springframework.dao.DataAccessException

evict

public void evict(java.lang.Object entity)
           throws org.springframework.dao.DataAccessException
Specified by:
evict in interface org.codehaus.groovy.grails.orm.hibernate.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 org.codehaus.groovy.grails.orm.hibernate.IHibernateTemplate
Throws:
org.springframework.dao.DataAccessException

refresh

public void refresh(java.lang.Object entity)
             throws org.springframework.dao.DataAccessException
Specified by:
refresh in interface org.codehaus.groovy.grails.orm.hibernate.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)
Prepare the given Query object, applying cache settings and/or a transaction timeout.

Parameters:
query - the Query object to prepare

prepareCriteria

protected void prepareCriteria(org.hibernate.Criteria criteria)
Prepare the given Criteria object, applying cache settings and/or a transaction timeout.

Parameters:
criteria - the Criteria 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 org.codehaus.groovy.grails.orm.hibernate.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 org.codehaus.groovy.grails.orm.hibernate.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 org.codehaus.groovy.grails.orm.hibernate.IHibernateTemplate

flush

public void flush()
Specified by:
flush in interface org.codehaus.groovy.grails.orm.hibernate.IHibernateTemplate

clear

public void clear()
Specified by:
clear in interface org.codehaus.groovy.grails.orm.hibernate.IHibernateTemplate

deleteAll

public void deleteAll(java.util.Collection<?> objects)
Specified by:
deleteAll in interface org.codehaus.groovy.grails.orm.hibernate.IHibernateTemplate

getIterableAsCollection

protected java.util.Collection getIterableAsCollection(java.lang.Iterable objects)