Class GrailsHibernateTemplate
- All Implemented Interfaces:
IHibernateTemplate
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classInvocation handler that suppresses close calls on Hibernate Sessions.static interface -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected DataSourceprotected booleanstatic final intFlushing before every query statement is rarely necessary.static final intAutomatic flushing is the default mode for a Hibernate Session.static final intFlushing 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 final intEager flushing leads to immediate synchronization with the database, even if in a transaction.static final intNever flush is a good strategy for read-only units of work.protected intprotected org.springframework.jdbc.support.SQLExceptionTranslatorprotected org.hibernate.SessionFactory -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedGrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory) GrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory, HibernateDatastore datastore) GrailsHibernateTemplate(org.hibernate.SessionFactory sessionFactory, HibernateDatastore datastore, int defaultFlushMode) -
Method Summary
Modifier and TypeMethodDescriptionprotected 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()booleanprotected 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.voidvoiddeleteAll(Collection<?> objects) protected <T> TdoExecute(GrailsHibernateTemplate.HibernateCallback<T> action, boolean enforceNativeSession) Execute the action specified by the given action object within a Session.void<T> Texecute(groovy.lang.Closure<T> callable) <T> Texecute(GrailsHibernateTemplate.HibernateCallback<T> action) List<?><T1> T1executeWithExistingOrCreateNewSession(org.hibernate.SessionFactory sessionFactory, groovy.lang.Closure<T1> callable) <T> TexecuteWithNewSession(groovy.lang.Closure<T> callable) voidflush()voidprotected voidflushIfNecessary(org.hibernate.Session session, boolean existingTransaction) <T> Tget(Class<T> entityClass, Serializable id) <T> Tget(Class<T> entityClass, Serializable id, org.hibernate.LockMode mode) intReturn if a flush should be forced after executing the callback code.protected CollectiongetIterableAsCollection(Iterable objects) protected org.hibernate.Sessionorg.hibernate.SessionFactorybooleanbooleanbooleanbooleanprotected booleanisSessionTransactional(org.hibernate.Session session) <T> Tload(Class<T> entityClass, Serializable id) <T> List<T><T> Tlock(Class<T> entityClass, Serializable id, org.hibernate.LockMode lockMode) voidprotected 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.voidvoidvoidsetApplyFlushModeOnlyToNonExistingTransactions(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 boolean
-
Field Details
-
exposeNativeSession
protected boolean exposeNativeSession -
cacheQueries
protected boolean cacheQueries -
sessionFactory
protected org.hibernate.SessionFactory sessionFactory -
dataSource
-
jdbcExceptionTranslator
protected org.springframework.jdbc.support.SQLExceptionTranslator jdbcExceptionTranslator -
flushMode
protected int flushMode -
FLUSH_NEVER
public static final int FLUSH_NEVERNever 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_AUTOAutomatic 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_EAGEREager 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_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.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_ALWAYSFlushing 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:
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 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(Class<T> entityClass, Serializable id) throws org.springframework.dao.DataAccessException - Specified by:
getin interfaceIHibernateTemplate- Throws:
org.springframework.dao.DataAccessException
-
get
- Specified by:
getin interfaceIHibernateTemplate
-
delete
- Specified by:
deletein interfaceIHibernateTemplate- Throws:
org.springframework.dao.DataAccessException
-
flush
- Throws:
org.springframework.dao.DataAccessException
-
load
public <T> T load(Class<T> entityClass, Serializable id) throws org.springframework.dao.DataAccessException - Specified by:
loadin interfaceIHibernateTemplate- 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
- Throws:
org.springframework.dao.DataAccessException
-
contains
- Specified by:
containsin interfaceIHibernateTemplate- Throws:
org.springframework.dao.DataAccessException
-
evict
- Specified by:
evictin interfaceIHibernateTemplate- Throws:
org.springframework.dao.DataAccessException
-
lock
public void lock(Object entity, org.hibernate.LockMode lockMode) throws org.springframework.dao.DataAccessException - Specified by:
lockin interfaceIHibernateTemplate- Throws:
org.springframework.dao.DataAccessException
-
refresh
- Specified by:
refreshin interfaceIHibernateTemplate- 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.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:
-
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
- Specified by:
savein interfaceIHibernateTemplate
-
flush
public void flush()- Specified by:
flushin interfaceIHibernateTemplate
-
clear
public void clear()- Specified by:
clearin interfaceIHibernateTemplate
-
deleteAll
- Specified by:
deleteAllin interfaceIHibernateTemplate
-
getIterableAsCollection
-
isApplyFlushModeOnlyToNonExistingTransactions
public boolean isApplyFlushModeOnlyToNonExistingTransactions() -
setApplyFlushModeOnlyToNonExistingTransactions
public void setApplyFlushModeOnlyToNonExistingTransactions(boolean applyFlushModeOnlyToNonExistingTransactions)
-