Class GrailsHibernateTemplate
- java.lang.Object
-
- org.grails.orm.hibernate.GrailsHibernateTemplate
-
- All Implemented Interfaces:
IHibernateTemplate
public class GrailsHibernateTemplate extends java.lang.Object implements IHibernateTemplate
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classGrailsHibernateTemplate.CloseSuppressingInvocationHandlerInvocation handler that suppresses close calls on Hibernate Sessions.static interfaceGrailsHibernateTemplate.HibernateCallback<T>
-
Field Summary
Fields Modifier and Type Field Description protected booleancacheQueriesprotected javax.sql.DataSourcedataSourceprotected booleanexposeNativeSessionstatic intFLUSH_ALWAYSFlushing before every query statement is rarely necessary.static intFLUSH_AUTOAutomatic flushing is the default mode for a Hibernate Session.static intFLUSH_COMMITFlushing 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 intFLUSH_EAGEREager flushing leads to immediate synchronization with the database, even if in a transaction.static intFLUSH_NEVERNever flush is a good strategy for read-only units of work.protected intflushModeprotected org.springframework.jdbc.support.SQLExceptionTranslatorjdbcExceptionTranslatorprotected org.hibernate.SessionFactorysessionFactory
-
Constructor Summary
Constructors Modifier Constructor Description protectedGrailsHibernateTemplate()GrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory)GrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory, HibernateDatastore datastore)GrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory, HibernateDatastore datastore, int defaultFlushMode)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected org.hibernate.FlushModeapplyFlushMode(org.hibernate.Session session, boolean existingTransaction)Apply the flush mode that's been specified for this accessor to the given Session.voidapplySettings(org.hibernate.Criteria criteria)voidapplySettings(org.hibernate.query.Query query)voidclear()booleancontains(java.lang.Object entity)protected org.springframework.dao.DataAccessExceptionconvertHibernateAccessException(org.hibernate.HibernateException ex)protected org.springframework.dao.DataAccessExceptionconvertJdbcAccessException(org.hibernate.JDBCException ex, org.springframework.jdbc.support.SQLExceptionTranslator translator)protected org.hibernate.SessioncreateSessionProxy(org.hibernate.Session session)Create a close-suppressing proxy for the given Hibernate Session.voiddelete(java.lang.Object entity)voiddeleteAll(java.util.Collection<?> objects)protected <T> TdoExecute(GrailsHibernateTemplate.HibernateCallback<T> action, boolean enforceNativeSession)Execute the action specified by the given action object within a Session.voidevict(java.lang.Object entity)<T> Texecute(groovy.lang.Closure<T> callable)<T> Texecute(GrailsHibernateTemplate.HibernateCallback<T> action)java.util.List<?>executeFind(GrailsHibernateTemplate.HibernateCallback<?> action)<T1> T1executeWithExistingOrCreateNewSession(org.hibernate.SessionFactory sessionFactory, groovy.lang.Closure<T1> callable)<T> TexecuteWithNewSession(groovy.lang.Closure<T> callable)voidflush()voidflush(java.lang.Object entity)protected voidflushIfNecessary(org.hibernate.Session session, boolean existingTransaction)<T> Tget(java.lang.Class<T> entityClass, java.io.Serializable id)<T> Tget(java.lang.Class<T> entityClass, java.io.Serializable id, org.hibernate.LockMode mode)intgetFlushMode()Return if a flush should be forced after executing the callback code.protected java.util.CollectiongetIterableAsCollection(java.lang.Iterable objects)protected org.hibernate.SessiongetSession()org.hibernate.SessionFactorygetSessionFactory()booleanisApplyFlushModeOnlyToNonExistingTransactions()booleanisCacheQueries()booleanisExposeNativeSession()booleanisOsivReadOnly()protected booleanisSessionTransactional(org.hibernate.Session session)<T> Tload(java.lang.Class<T> entityClass, java.io.Serializable id)<T> java.util.List<T>loadAll(java.lang.Class<T> entityClass)<T> Tlock(java.lang.Class<T> entityClass, java.io.Serializable id, org.hibernate.LockMode lockMode)voidlock(java.lang.Object entity, org.hibernate.LockMode lockMode)protected voidprepareCriteria(org.hibernate.Criteria criteria)Deprecated.Deprecated because Hibernate Criteria are deprecatedprotected <T> voidprepareCriteria(org.hibernate.query.Query<T> jpaQuery)Prepare the given Query object, applying cache settings and/or a transaction timeout.protected voidprepareQuery(org.hibernate.query.Query query)Prepare the given Query object, applying cache settings and/or a transaction timeout.voidrefresh(java.lang.Object entity)voidrefresh(java.lang.Object entity, org.hibernate.LockMode lockMode)java.io.Serializablesave(java.lang.Object o)voidsetApplyFlushModeOnlyToNonExistingTransactions(boolean applyFlushModeOnlyToNonExistingTransactions)voidsetCacheQueries(boolean cacheQueries)voidsetExposeNativeSession(boolean exposeNativeSession)voidsetFlushMode(int flushMode)Set the flush behavior to one of the constants in this class.voidsetOsivReadOnly(boolean osivReadOnly)protected booleanshouldPassReadOnlyToHibernate()
-
-
-
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:
executein interfaceIHibernateTemplate
-
executeWithNewSession
public <T> T executeWithNewSession(groovy.lang.Closure<T> callable)
- Specified by:
executeWithNewSessionin interfaceIHibernateTemplate
-
executeWithExistingOrCreateNewSession
public <T1> T1 executeWithExistingOrCreateNewSession(org.hibernate.SessionFactory sessionFactory, groovy.lang.Closure<T1> callable)- Specified by:
executeWithExistingOrCreateNewSessionin interfaceIHibernateTemplate
-
getSessionFactory
public org.hibernate.SessionFactory getSessionFactory()
- Specified by:
getSessionFactoryin interfaceIHibernateTemplate
-
applySettings
public void applySettings(org.hibernate.query.Query query)
- Specified by:
applySettingsin interfaceIHibernateTemplate
-
applySettings
public void applySettings(org.hibernate.Criteria criteria)
- Specified by:
applySettingsin interfaceIHibernateTemplate
-
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
-
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 actionenforceNativeSession- 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:
getin interfaceIHibernateTemplate- 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:
getin interfaceIHibernateTemplate
-
delete
public void delete(java.lang.Object entity) throws org.springframework.dao.DataAccessException- Specified by:
deletein interfaceIHibernateTemplate- 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:
loadin interfaceIHibernateTemplate- 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:
containsin interfaceIHibernateTemplate- Throws:
org.springframework.dao.DataAccessException
-
evict
public void evict(java.lang.Object entity) throws org.springframework.dao.DataAccessException- Specified by:
evictin interfaceIHibernateTemplate- Throws:
org.springframework.dao.DataAccessException
-
lock
public void lock(java.lang.Object entity, org.hibernate.LockMode lockMode) throws org.springframework.dao.DataAccessException- Specified by:
lockin interfaceIHibernateTemplate- Throws:
org.springframework.dao.DataAccessException
-
refresh
public void refresh(java.lang.Object entity) throws org.springframework.dao.DataAccessException- Specified by:
refreshin interfaceIHibernateTemplate- 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 deprecatedPrepare 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:
setFlushModein interfaceIHibernateTemplate- See Also:
FLUSH_AUTO
-
getFlushMode
public int getFlushMode()
Return if a flush should be forced after executing the callback code.- Specified by:
getFlushModein interfaceIHibernateTemplate
-
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 SessionexistingTransaction- if executing within an existing transaction- Returns:
- the previous flush mode to restore after the operation, or
nullif 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:
savein interfaceIHibernateTemplate
-
flush
public void flush()
- Specified by:
flushin interfaceIHibernateTemplate
-
clear
public void clear()
- Specified by:
clearin interfaceIHibernateTemplate
-
deleteAll
public void deleteAll(java.util.Collection<?> objects)
- Specified by:
deleteAllin interfaceIHibernateTemplate
-
getIterableAsCollection
protected java.util.Collection getIterableAsCollection(java.lang.Iterable objects)
-
isApplyFlushModeOnlyToNonExistingTransactions
public boolean isApplyFlushModeOnlyToNonExistingTransactions()
-
setApplyFlushModeOnlyToNonExistingTransactions
public void setApplyFlushModeOnlyToNonExistingTransactions(boolean applyFlushModeOnlyToNonExistingTransactions)
-
-