Typically used to implement data access or business logic services that
- /// use NHibernate within their implementation but are Hibernate-agnostic in their
- /// interface. The latter or code calling the latter only have to deal with
- /// domain objects.
- ///
- ///
The central method is Execute supporting Hibernate access code
- /// implementing the HibernateCallback interface. It provides NHibernate Session
- /// handling such that neither the IHibernateCallback implementation nor the calling
- /// code needs to explicitly care about retrieving/closing NHibernate Sessions,
- /// or handling Session lifecycle exceptions. For typical single step actions,
- /// there are various convenience methods (Find, Load, SaveOrUpdate, Delete).
- ///
- ///
- ///
Can be used within a service implementation via direct instantiation
- /// with a ISessionFactory reference, or get prepared in an application context
- /// and given to services as an object reference. Note: The ISessionFactory should
- /// always be configured as an object in the application context, in the first case
- /// given to the service directly, in the second case to the prepared template.
- ///
- ///
- ///
This class can be considered as direct alternative to working with the raw
- /// Hibernate Session API (through SessionFactoryUtils.Session).
- ///
- ///
- ///
LocalSessionFactoryObject is the preferred way of obtaining a reference
- /// to a specific NHibernate ISessionFactory.
- ///
- ///
- /// Mark Pollack (.NET)
- public class HibernateTemplate : HibernateAccessor, IHibernateOperations
- {
- #region Fields
-
- ///
- /// The instance for this class.
- ///
- private readonly ILog log = LogManager.GetLogger(typeof(HibernateTemplate));
-
- private bool checkWriteOperations = true;
-
-
- private bool exposeNativeSession = false;
-
- private bool alwaysUseNewSession = false;
- private int maxResults = 0;
- private TemplateFlushMode templateFlushMode = TemplateFlushMode.Auto;
- private bool allowCreate = true;
- private ISessionFactory sessionFactory;
- private object entityInterceptor;
- private IObjectFactory objectFactory;
- private bool cacheQueries = false;
- private string queryCacheRegion;
- private int fetchSize = 0;
-
- private IAdoExceptionTranslator adoExceptionTranslator;
-
- private readonly object syncRoot = new object();
- private ProxyFactory sessionProxyFactory;
-
- #endregion
-
- #region Constructor (s)
- ///
- /// Initializes a new instance of the class.
- ///
- public HibernateTemplate()
- {
-
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The default for creating a new non-transactional
- /// session when no transactional Session can be found for the current thread
- /// is set to true.
- /// The session factory to create sessions.
- public HibernateTemplate(ISessionFactory sessionFactory)
- {
- SessionFactory = sessionFactory;
- AfterPropertiesSet();
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The session factory to create sessions.
- /// if set to true allow creation
- /// of a new non-transactional when no transactional Session can be found
- /// for the current thread.
- public HibernateTemplate(ISessionFactory sessionFactory, bool allowCreate)
- {
- SessionFactory = sessionFactory;
- AllowCreate = allowCreate;
- AfterPropertiesSet();
- }
- #endregion
-
- #region Properties
-
- ///
- /// Gets or sets if a new Session should be created when no transactional Session
- /// can be found for the current thread.
- ///
- ///
- /// true if allowed to create non-transaction session;
- /// otherwise, false.
- ///
- ///
- ///
HibernateTemplate is aware of a corresponding Session bound to the
- /// current thread, for example when using HibernateTransactionManager.
- /// If allowCreate is true, a new non-transactional Session will be created
- /// if none found, which needs to be closed at the end of the operation.
- /// If false, an InvalidOperationException will get thrown in this case.
- ///
- ///
- public override bool AllowCreate
- {
- get
- {
-
- return allowCreate;
- }
- set { allowCreate = value; }
- }
-
- ///
- /// Gets or sets a value indicating whether to always
- /// use a new Hibernate Session for this template.
- ///
- /// true if always use new session; otherwise, false.
- ///
- ///
- /// Default is "false"; if activated, all operations on this template will
- /// work on a new NHibernate ISession even in case of a pre-bound ISession
- /// (for example, within a transaction).
- ///
- ///
Within a transaction, a new NHibernate ISession used by this template
- /// will participate in the transaction through using the same ADO.NET
- /// Connection. In such a scenario, multiple Sessions will participate
- /// in the same database transaction.
- ///
- ///
Turn this on for operations that are supposed to always execute
- /// independently, without side effects caused by a shared NHibernate ISession.
- ///
- ///
- public override bool AlwaysUseNewSession
- {
- get { return alwaysUseNewSession; }
- set { alwaysUseNewSession = value; }
- }
-
-
- ///
- /// Gets or sets the template flush mode.
- ///
- ///
- /// Default is Auto. Will get applied to any new ISession
- /// created by the template.
- ///
- /// The template flush mode.
- public override TemplateFlushMode TemplateFlushMode
- {
- get { return templateFlushMode; }
- set { templateFlushMode = value; }
- }
-
- ///
- /// Gets or sets the entity interceptor that allows to inspect and change
- /// property values before writing to and reading from the database.
- ///
- ///
- /// Will get applied to any new ISession created by this object.
- ///
Such an interceptor can either be set at the ISessionFactory level,
- /// i.e. on LocalSessionFactoryObject, or at the ISession level, i.e. on
- /// HibernateTemplate, HibernateInterceptor, and HibernateTransactionManager.
- /// It's preferable to set it on LocalSessionFactoryObject or HibernateTransactionManager
- /// to avoid repeated configuration and guarantee consistent behavior in transactions.
- ///
- ///
- /// The interceptor.
- /// If object factory is not set and need to retrieve entity interceptor by name.
- public override IInterceptor EntityInterceptor
- {
- get
- {
- if (this.entityInterceptor is string)
- {
- if (this.objectFactory == null)
- {
- throw new InvalidOperationException("Cannot get entity interceptor via object name if no object factory set");
- }
- return (IInterceptor)this.objectFactory.GetObject((String)this.entityInterceptor, typeof(IInterceptor));
- }
-
- return (IInterceptor)entityInterceptor;
- }
- set
- {
- entityInterceptor = value;
- }
- }
- ///
- /// Gets or sets the name of the cache region for queries executed by this template.
- ///
- ///
- /// If this is specified, it will be applied to all IQuery and ICriteria objects
- /// created by this template (including all queries through find methods).
- ///
The cache region will not take effect unless queries created by this
- /// template are configured to be cached via the CacheQueries property.
- ///
- ///
- /// The query cache region.
- public override string QueryCacheRegion
- {
- get { return queryCacheRegion; }
- set { queryCacheRegion = value; }
- }
-
-
- ///
- /// Gets or sets a value indicating whether to
- /// cache all queries executed by this template.
- ///
- ///
- /// If this is true, all IQuery and ICriteria objects created by
- /// this template will be marked as cacheable (including all
- /// queries through find methods).
- ///
To specify the query region to be used for queries cached
- /// by this template, set the QueryCacheRegion property.
- ///
- ///
- /// true if cache queries; otherwise, false.
- public override bool CacheQueries
- {
- get { return cacheQueries; }
- set { cacheQueries = value; }
- }
-
- ///
- /// Gets or sets the maximum number of rows for this HibernateTemplate.
- ///
- /// The max results.
- ///
- /// This is important
- /// for processing subsets of large result sets, avoiding to read and hold
- /// the entire result set in the database or in the ADO.NET driver if we're
- /// never interested in the entire result in the first place (for example,
- /// when performing searches that might return a large number of matches).
- ///
Default is 0, indicating to use the driver's default.
- ///
- public override int MaxResults
- {
- get { return maxResults; }
- set { maxResults = value; }
- }
-
- ///
- /// Set whether to expose the native Hibernate Session to IHibernateCallback
- /// code. Default is "false": a Session proxy will be returned,
- /// suppressing close calls and automatically applying
- /// query cache settings and transaction timeouts.
- ///
- /// true if expose native session; otherwise, false.
- public override bool ExposeNativeSession
- {
- get { return exposeNativeSession; }
- set { exposeNativeSession = value; }
- }
- ///
- /// Gets or sets whether to check that the Hibernate Session is not in read-only mode
- /// in case of write operations (save/update/delete).
- ///
- ///
- /// true if check that the Hibernate Session is not in read-only mode
- /// in case of write operations; otherwise, false.
- ///
- ///
- /// Default is "true", for fail-fast behavior when attempting write operations
- /// within a read-only transaction. Turn this off to allow save/update/delete
- /// on a Session with flush mode NEVER.
- ///
- public virtual bool CheckWriteOperations
- {
- get { return checkWriteOperations; }
- set { checkWriteOperations = value; }
- }
-
- ///
- /// Set the object name of a Hibernate entity interceptor that allows to inspect
- /// and change property values before writing to and reading from the database.
- ///
- ///
- /// Will get applied to any new Session created by this transaction manager.
- ///
Requires the object factory to be known, to be able to resolve the object
- /// name to an interceptor instance on session creation. Typically used for
- /// prototype interceptors, i.e. a new interceptor instance per session.
- ///
- ///
Can also be used for shared interceptor instances, but it is recommended
- /// to set the interceptor reference directly in such a scenario.
- ///
- ///
- /// The name of the entity interceptor in the object factory/application context.
- public override string EntityInterceptorObjectName
- {
- set
- {
- this.entityInterceptor = value;
- }
- }
-
- ///
- /// Set the object factory instance.
- ///
- /// The object factory instance
- public override IObjectFactory ObjectFactory
- {
- set
- {
- objectFactory = value;
- }
- }
-
- ///
- /// Gets or sets the session factory that should be used to create
- /// NHibernate ISessions.
- ///
- /// The session factory.
- public override ISessionFactory SessionFactory
- {
- get { return sessionFactory; }
- set
- {
- sessionFactory = value;
- }
- }
-
- ///
- /// Gets or sets the fetch size for this HibernateTemplate.
- ///
- /// The size of the fetch.
- /// This is important for processing
- /// large result sets: Setting this higher than the default value will increase
- /// processing speed at the cost of memory consumption; setting this lower can
- /// avoid transferring row data that will never be read by the application.
- ///
Default is 0, indicating to use the driver's default.
- ///
- public override int FetchSize
- {
- get { return fetchSize; }
- set { fetchSize = value; }
- }
-
-
- ///
- /// Gets or sets the proxy factory.
- ///
- /// This may be useful to set if you create many instances of
- /// HibernateTemplate and/or HibernateDaoSupport. This allows the same
- /// ProxyFactory implementation to be used thereby limiting the
- /// number of dynamic proxy types created in the temporary assembly, which
- /// are never garbage collected due to .NET runtime semantics.
- ///
- /// The proxy factory.
- public virtual ProxyFactory ProxyFactory
- {
- get { return sessionProxyFactory; }
- set { sessionProxyFactory = value; }
- }
-
- #endregion
-
- #region IHibernateOperations Members
-
- ///
- /// Set the ADO.NET exception translator for this instance.
- /// Applied to System.Data.Common.DbException (or provider specific exception type
- /// in .NET 1.1) thrown by callback code, be it direct
- /// DbException or wrapped Hibernate ADOExceptions.
- ///
The default exception translator is either a ErrorCodeExceptionTranslator
- /// if a DbProvider is available, or a FalbackExceptionTranslator otherwise
- ///
Typically used to implement data access or business logic services that
+ /// use NHibernate within their implementation but are Hibernate-agnostic in their
+ /// interface. The latter or code calling the latter only have to deal with
+ /// domain objects.
+ ///
+ ///
The central method is Execute supporting Hibernate access code
+ /// implementing the HibernateCallback interface. It provides NHibernate Session
+ /// handling such that neither the IHibernateCallback implementation nor the calling
+ /// code needs to explicitly care about retrieving/closing NHibernate Sessions,
+ /// or handling Session lifecycle exceptions. For typical single step actions,
+ /// there are various convenience methods (Find, Load, SaveOrUpdate, Delete).
+ ///
+ ///
+ ///
Can be used within a service implementation via direct instantiation
+ /// with a ISessionFactory reference, or get prepared in an application context
+ /// and given to services as an object reference. Note: The ISessionFactory should
+ /// always be configured as an object in the application context, in the first case
+ /// given to the service directly, in the second case to the prepared template.
+ ///
+ ///
+ ///
This class can be considered as direct alternative to working with the raw
+ /// Hibernate Session API (through SessionFactoryUtils.Session).
+ ///
+ ///
+ ///
LocalSessionFactoryObject is the preferred way of obtaining a reference
+ /// to a specific NHibernate ISessionFactory.
+ ///
+ ///
+ /// Mark Pollack (.NET)
+ public class HibernateTemplate : HibernateAccessor, IHibernateOperations
+ {
+ #region Fields
+
+ ///
+ /// The instance for this class.
+ ///
+ private readonly ILog log = LogManager.GetLogger(typeof(HibernateTemplate));
+
+ private bool checkWriteOperations = true;
+
+
+ private bool exposeNativeSession = false;
+
+ private bool alwaysUseNewSession = false;
+ private int maxResults = 0;
+ private TemplateFlushMode templateFlushMode = TemplateFlushMode.Auto;
+ private bool allowCreate = true;
+ private ISessionFactory sessionFactory;
+ private object entityInterceptor;
+ private IObjectFactory objectFactory;
+ private bool cacheQueries = false;
+ private string queryCacheRegion;
+ private int fetchSize = 0;
+
+ private IAdoExceptionTranslator adoExceptionTranslator;
+
+ private readonly object syncRoot = new object();
+ private ProxyFactory sessionProxyFactory;
+
+ #endregion
+
+ #region Constructor (s)
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public HibernateTemplate()
+ {
+
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The default for creating a new non-transactional
+ /// session when no transactional Session can be found for the current thread
+ /// is set to true.
+ /// The session factory to create sessions.
+ public HibernateTemplate(ISessionFactory sessionFactory)
+ {
+ SessionFactory = sessionFactory;
+ AfterPropertiesSet();
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The session factory to create sessions.
+ /// if set to true allow creation
+ /// of a new non-transactional when no transactional Session can be found
+ /// for the current thread.
+ public HibernateTemplate(ISessionFactory sessionFactory, bool allowCreate)
+ {
+ SessionFactory = sessionFactory;
+ AllowCreate = allowCreate;
+ AfterPropertiesSet();
+ }
+ #endregion
+
+ #region Properties
+
+ ///
+ /// Gets or sets if a new Session should be created when no transactional Session
+ /// can be found for the current thread.
+ ///
+ ///
+ /// true if allowed to create non-transaction session;
+ /// otherwise, false.
+ ///
+ ///
+ ///
HibernateTemplate is aware of a corresponding Session bound to the
+ /// current thread, for example when using HibernateTransactionManager.
+ /// If allowCreate is true, a new non-transactional Session will be created
+ /// if none found, which needs to be closed at the end of the operation.
+ /// If false, an InvalidOperationException will get thrown in this case.
+ ///
+ ///
+ public override bool AllowCreate
+ {
+ get
+ {
+
+ return allowCreate;
+ }
+ set { allowCreate = value; }
+ }
+
+ ///
+ /// Gets or sets a value indicating whether to always
+ /// use a new Hibernate Session for this template.
+ ///
+ /// true if always use new session; otherwise, false.
+ ///
+ ///
+ /// Default is "false"; if activated, all operations on this template will
+ /// work on a new NHibernate ISession even in case of a pre-bound ISession
+ /// (for example, within a transaction).
+ ///
+ ///
Within a transaction, a new NHibernate ISession used by this template
+ /// will participate in the transaction through using the same ADO.NET
+ /// Connection. In such a scenario, multiple Sessions will participate
+ /// in the same database transaction.
+ ///
+ ///
Turn this on for operations that are supposed to always execute
+ /// independently, without side effects caused by a shared NHibernate ISession.
+ ///
+ ///
+ public override bool AlwaysUseNewSession
+ {
+ get { return alwaysUseNewSession; }
+ set { alwaysUseNewSession = value; }
+ }
+
+
+ ///
+ /// Gets or sets the template flush mode.
+ ///
+ ///
+ /// Default is Auto. Will get applied to any new ISession
+ /// created by the template.
+ ///
+ /// The template flush mode.
+ public override TemplateFlushMode TemplateFlushMode
+ {
+ get { return templateFlushMode; }
+ set { templateFlushMode = value; }
+ }
+
+ ///
+ /// Gets or sets the entity interceptor that allows to inspect and change
+ /// property values before writing to and reading from the database.
+ ///
+ ///
+ /// Will get applied to any new ISession created by this object.
+ ///
Such an interceptor can either be set at the ISessionFactory level,
+ /// i.e. on LocalSessionFactoryObject, or at the ISession level, i.e. on
+ /// HibernateTemplate, HibernateInterceptor, and HibernateTransactionManager.
+ /// It's preferable to set it on LocalSessionFactoryObject or HibernateTransactionManager
+ /// to avoid repeated configuration and guarantee consistent behavior in transactions.
+ ///
+ ///
+ /// The interceptor.
+ /// If object factory is not set and need to retrieve entity interceptor by name.
+ public override IInterceptor EntityInterceptor
+ {
+ get
+ {
+ if (this.entityInterceptor is string)
+ {
+ if (this.objectFactory == null)
+ {
+ throw new InvalidOperationException("Cannot get entity interceptor via object name if no object factory set");
+ }
+ return (IInterceptor)this.objectFactory.GetObject((String)this.entityInterceptor, typeof(IInterceptor));
+ }
+
+ return (IInterceptor)entityInterceptor;
+ }
+ set
+ {
+ entityInterceptor = value;
+ }
+ }
+ ///
+ /// Gets or sets the name of the cache region for queries executed by this template.
+ ///
+ ///
+ /// If this is specified, it will be applied to all IQuery and ICriteria objects
+ /// created by this template (including all queries through find methods).
+ ///
The cache region will not take effect unless queries created by this
+ /// template are configured to be cached via the CacheQueries property.
+ ///
+ ///
+ /// The query cache region.
+ public override string QueryCacheRegion
+ {
+ get { return queryCacheRegion; }
+ set { queryCacheRegion = value; }
+ }
+
+
+ ///
+ /// Gets or sets a value indicating whether to
+ /// cache all queries executed by this template.
+ ///
+ ///
+ /// If this is true, all IQuery and ICriteria objects created by
+ /// this template will be marked as cacheable (including all
+ /// queries through find methods).
+ ///
To specify the query region to be used for queries cached
+ /// by this template, set the QueryCacheRegion property.
+ ///
+ ///
+ /// true if cache queries; otherwise, false.
+ public override bool CacheQueries
+ {
+ get { return cacheQueries; }
+ set { cacheQueries = value; }
+ }
+
+ ///
+ /// Gets or sets the maximum number of rows for this HibernateTemplate.
+ ///
+ /// The max results.
+ ///
+ /// This is important
+ /// for processing subsets of large result sets, avoiding to read and hold
+ /// the entire result set in the database or in the ADO.NET driver if we're
+ /// never interested in the entire result in the first place (for example,
+ /// when performing searches that might return a large number of matches).
+ ///
Default is 0, indicating to use the driver's default.
+ ///
+ public override int MaxResults
+ {
+ get { return maxResults; }
+ set { maxResults = value; }
+ }
+
+ ///
+ /// Set whether to expose the native Hibernate Session to IHibernateCallback
+ /// code. Default is "false": a Session proxy will be returned,
+ /// suppressing close calls and automatically applying
+ /// query cache settings and transaction timeouts.
+ ///
+ /// true if expose native session; otherwise, false.
+ public override bool ExposeNativeSession
+ {
+ get { return exposeNativeSession; }
+ set { exposeNativeSession = value; }
+ }
+ ///
+ /// Gets or sets whether to check that the Hibernate Session is not in read-only mode
+ /// in case of write operations (save/update/delete).
+ ///
+ ///
+ /// true if check that the Hibernate Session is not in read-only mode
+ /// in case of write operations; otherwise, false.
+ ///
+ ///
+ /// Default is "true", for fail-fast behavior when attempting write operations
+ /// within a read-only transaction. Turn this off to allow save/update/delete
+ /// on a Session with flush mode NEVER.
+ ///
+ public virtual bool CheckWriteOperations
+ {
+ get { return checkWriteOperations; }
+ set { checkWriteOperations = value; }
+ }
+
+ ///
+ /// Set the object name of a Hibernate entity interceptor that allows to inspect
+ /// and change property values before writing to and reading from the database.
+ ///
+ ///
+ /// Will get applied to any new Session created by this transaction manager.
+ ///
Requires the object factory to be known, to be able to resolve the object
+ /// name to an interceptor instance on session creation. Typically used for
+ /// prototype interceptors, i.e. a new interceptor instance per session.
+ ///
+ ///
Can also be used for shared interceptor instances, but it is recommended
+ /// to set the interceptor reference directly in such a scenario.
+ ///
+ ///
+ /// The name of the entity interceptor in the object factory/application context.
+ public override string EntityInterceptorObjectName
+ {
+ set
+ {
+ this.entityInterceptor = value;
+ }
+ }
+
+ ///
+ /// Set the object factory instance.
+ ///
+ /// The object factory instance
+ public override IObjectFactory ObjectFactory
+ {
+ set
+ {
+ objectFactory = value;
+ }
+ }
+
+ ///
+ /// Gets or sets the session factory that should be used to create
+ /// NHibernate ISessions.
+ ///
+ /// The session factory.
+ public override ISessionFactory SessionFactory
+ {
+ get { return sessionFactory; }
+ set
+ {
+ sessionFactory = value;
+ }
+ }
+
+ ///
+ /// Gets or sets the fetch size for this HibernateTemplate.
+ ///
+ /// The size of the fetch.
+ /// This is important for processing
+ /// large result sets: Setting this higher than the default value will increase
+ /// processing speed at the cost of memory consumption; setting this lower can
+ /// avoid transferring row data that will never be read by the application.
+ ///
Default is 0, indicating to use the driver's default.
+ ///
+ public override int FetchSize
+ {
+ get { return fetchSize; }
+ set { fetchSize = value; }
+ }
+
+
+ ///
+ /// Gets or sets the proxy factory.
+ ///
+ /// This may be useful to set if you create many instances of
+ /// HibernateTemplate and/or HibernateDaoSupport. This allows the same
+ /// ProxyFactory implementation to be used thereby limiting the
+ /// number of dynamic proxy types created in the temporary assembly, which
+ /// are never garbage collected due to .NET runtime semantics.
+ ///
+ /// The proxy factory.
+ public virtual ProxyFactory ProxyFactory
+ {
+ get { return sessionProxyFactory; }
+ set { sessionProxyFactory = value; }
+ }
+
+ #endregion
+
+ #region IHibernateOperations Members
+
+ ///
+ /// Set the ADO.NET exception translator for this instance.
+ /// Applied to System.Data.Common.DbException (or provider specific exception type
+ /// in .NET 1.1) thrown by callback code, be it direct
+ /// DbException or wrapped Hibernate ADOExceptions.
+ ///
The default exception translator is either a ErrorCodeExceptionTranslator
+ /// if a DbProvider is available, or a FalbackExceptionTranslator otherwise
+ ///
+ ///
+ /// The ADO exception translator.
+ public override IAdoExceptionTranslator AdoExceptionTranslator
+ {
+ set { adoExceptionTranslator = value; }
+ get
+ {
+ if (adoExceptionTranslator == null)
+ {
+ adoExceptionTranslator = SessionFactoryUtils.NewAdoExceptionTranslator(SessionFactory);
+ }
+ return adoExceptionTranslator;
+ }
+ }
+
+
+ ///
+ /// Delegate function that clears the session.
+ ///
+ /// The hibernate session.
+ /// null
+ protected object ClearAction(ISession session)
+ {
+ session.Clear();
+ return null;
+ }
+
+ ///
+ /// Flush all pending saves, updates and deletes to the database.
+ ///
+ ///
+ /// Only invoke this for selective eager flushing, for example when ADO.NET code
+ /// needs to see certain changes within the same transaction. Else, it's preferable
+ /// to rely on auto-flushing at transaction completion.
+ ///
+ /// In case of Hibernate errors
+ public void Flush()
+ {
+ Execute(new HibernateDelegate(FlushAction), true);
+ }
+
+ private object FlushAction(ISession session)
+ {
+ session.Flush();
+ return null;
+ }
+
+ ///
+ /// Return the persistent instance of the given entity type
+ /// with the given identifier, or null if not found.
+ ///
+ /// The type.
+ /// An identifier of the persistent instance.
+ /// The persistent instance, or null if not found
+ /// In case of Hibernate errors
+ public object Get(Type entityType, object id)
+ {
+ return Get(entityType, id, null);
+ }
+
+ ///
+ /// Return the persistent instance of the given entity type
+ /// with the given identifier, or null if not found.
+ /// Obtains the specified lock mode if the instance exists.
+ ///
+ /// The type.
+ /// The lock mode to obtain.
+ /// The lock mode.
+ /// the persistent instance, or null if not found
+ /// the persistent instance, or null if not found
+ /// In case of Hibernate errors
+ public object Get(Type type, object id, LockMode lockMode)
+ {
+ return Execute(new GetByTypeHibernateCallback(type, id, lockMode),true);
+
+ }
+
+ ///
+ /// Return the persistent instance of the given entity class
+ /// with the given identifier, throwing an exception if not found.
+ ///
+ /// Type of the entity.
+ /// An identifier of the persistent instance.
+ /// The persistent instance
+ /// If not found
+ /// In case of Hibernate errors
+ public object Load(Type entityType, object id)
+ {
+ return Load(entityType, id, null);
+ }
+
+ ///
+ /// Return the persistent instance of the given entity class
+ /// with the given identifier, throwing an exception if not found.
+ /// Obtains the specified lock mode if the instance exists.
+ ///
+ /// Type of the entity.
+ /// An identifier of the persistent instance.
+ /// The lock mode.
+ /// The persistent instance
+ /// If not found
+ /// In case of Hibernate errors
+ public object Load(Type entityType, object id, LockMode lockMode)
+ {
+ return Execute(new LoadByTypeHibernateCallback(entityType, id, lockMode),true);
+
+ }
+
+ ///
+ /// Load the persistent instance with the given identifier
+ /// into the given object, throwing an exception if not found.
+ ///
+ /// Entity the object (of the target class) to load into.
+ /// An identifier of the persistent instance.
+ /// If object not found.
+ /// In case of Hibernate errors
+ public void Load(object entity, object id)
+ {
+ Execute(new LoadByEntityHibernateCallback(entity, id),true);
+ }
+
+ ///
+ /// Return all persistent instances of the given entity class.
+ /// Note: Use queries or criteria for retrieving a specific subset.
+ ///
+ /// Type of the entity.
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList LoadAll(Type entityType)
+ {
+ return (IList)Execute(new LoadAllByTypeHibernateCallback(this, entityType),true);
+ }
+
+ ///
+ /// Re-read the state of the given persistent instance.
+ ///
+ /// The persistent instance to re-read.
+ /// In case of Hibernate errors
+ public void Refresh(object entity)
+ {
+ Refresh(entity, null);
+ }
+
+ ///
+ /// Re-read the state of the given persistent instance.
+ /// Obtains the specified lock mode for the instance.
+ ///
+ /// The persistent instance to re-read.
+ /// The lock mode to obtain.
+ /// In case of Hibernate errors
+ public void Refresh(object entity, LockMode lockMode)
+ {
+ Execute(new RefreshHibernateCallback(entity, lockMode),true);
+ }
+
+ ///
+ /// Obtain the specified lock level upon the given object, implicitly
+ /// checking whether the corresponding database entry still exists
+ /// (throwing an OptimisticLockingFailureException if not found).
+ ///
+ /// The he persistent instance to lock.
+ /// The lock mode to obtain.
+ /// If not found
+ /// In case of Hibernate errors
+ public void Lock(object entity, LockMode lockMode)
+ {
+ Execute(new LockHibernateCallback(entity, lockMode),true);
+ }
+
+ ///
+ /// Persist the given transient instance.
+ ///
+ /// The transient instance to persist.
+ /// The generated identifier.
+ /// In case of Hibernate errors
+ public object Save(object entity)
+ {
+ return Execute(new SaveObjectHibernateCallback(this, entity),true);
+ }
+
+ ///
+ /// Persist the given transient instance with the given identifier.
+ ///
+ /// The transient instance to persist.
+ /// The identifier to assign.
+ /// In case of Hibernate errors
+ public void Save(object entity, object id)
+ {
+ Execute(new SaveObjectWithIdHibernateCallback(this, entity, id),true);
+ }
+
+ ///
+ /// Update the given persistent instance.
+ ///
+ /// The persistent instance to update.
+ /// In case of Hibernate errors
+ public void Update(object entity)
+ {
+ Update(entity, null);
+ }
+
+ ///
+ /// Update the given persistent instance.
+ /// Obtains the specified lock mode if the instance exists, implicitly
+ /// checking whether the corresponding database entry still exists
+ /// (throwing an OptimisticLockingFailureException if not found).
+ ///
+ /// The persistent instance to update.
+ /// The lock mode to obtain.
+ /// In case of Hibernate errors
+ public void Update(object entity, LockMode lockMode)
+ {
+ Execute(new UpdateObjectHibernateCallback(this, entity, lockMode),true);
+ }
+
+ ///
+ /// Save or update the given persistent instance,
+ /// according to its id (matching the configured "unsaved-value"?).
+ ///
+ /// Tthe persistent instance to save or update
+ /// (to be associated with the Hibernate Session).
+ /// In case of Hibernate errors
+ public void SaveOrUpdate(object entity)
+ {
+ Execute(new SaveOrUpdateObjectHibernateCallback(this, entity),true);
+ }
+
+ ///
+ /// Save or update all given persistent instances,
+ /// according to its id (matching the configured "unsaved-value"?).
+ ///
+ /// Tthe persistent instances to save or update
+ /// (to be associated with the Hibernate Session)he entities.
+ /// In case of Hibernate errors
+ public void SaveOrUpdateAll(ICollection entities)
+ {
+ Execute(new SaveOrUpdateAllHibernateCallback(this, entities), true);
+ }
+
+ ///
+ /// Save or update the contents of given persistent object,
+ /// according to its id (matching the configured "unsaved-value"?).
+ /// Will copy the contained fields to an already loaded instance
+ /// with the same id, if appropriate.
+ ///
+ /// The persistent object to save or update.
+ /// (not necessarily to be associated with the Hibernate Session)
+ ///
+ /// The actually associated persistent object.
+ /// (either an already loaded instance with the same id, or the given object)
+ /// In case of Hibernate errors
+ public object SaveOrUpdateCopy(object entity)
+ {
+ return Execute(new SaveOrUpdateCopyHibernateCallback(this, entity),true);
}
#if !NH_1_2
@@ -697,1557 +697,1560 @@ namespace Spring.Data.NHibernate
public object Merge(object entity)
{
return Execute(new MergeHibernateCallback(this, entity), true);
- }
-#endif
-
- ///
- /// Remove all objects from the Session cache, and cancel all pending saves,
- /// updates and deletes.
- ///
- public void Clear()
- {
- Execute(new HibernateDelegate(ClearAction), true);
- }
-
-
-
- ///
- /// Determines whether the given object is in the Session cache.
- ///
- /// the persistence instance to check.
- ///
- /// true if session cache contains the specified entity; otherwise, false.
- ///
- /// In case of Hibernate errors
- public bool Contains(object entity)
- {
- return (bool)Execute(new ContainsHibernateCallback(entity));
- }
-
- ///
- /// Remove the given object from the Session cache.
- ///
- /// The persistent instance to evict.
- /// In case of Hibernate errors
- public void Evict(object entity)
- {
- Execute(new EvictHibernateCallback(entity), true);
-
- }
-
-
-
- ///
- /// Delete the given persistent instance.
- ///
- /// The persistent instance to delete.
- /// In case of Hibernate errors
- public void Delete(object entity)
- {
- Delete(entity, null);
- }
-
-
- ///
- /// Delete the given persistent instance.
- ///
- /// Tthe persistent instance to delete.
- /// The lock mode to obtain.
- ///
- /// Obtains the specified lock mode if the instance exists, implicitly
- /// checking whether the corresponding database entry still exists
- /// (throwing an OptimisticLockingFailureException if not found).
- ///
- /// In case of Hibernate errors
- public void Delete(object entity, LockMode lockMode)
- {
- Execute(new DeleteLockModeHibernateCallback(this, entity, lockMode), true);
- }
-
- ///
- /// Delete all objects returned by the query.
- ///
- /// a query expressed in Hibernate's query language.
- /// The number of entity instances deleted.
- /// In case of Hibernate errors
- public int Delete(string queryString)
- {
- return Delete(queryString, (Object[]) null, (IType[]) null);
- }
-
- ///
- /// Delete all objects returned by the query.
- ///
- /// a query expressed in Hibernate's query language.
- /// The value of the parameter.
- /// The Hibernate type of the parameter (or null).
- /// The number of entity instances deleted.
- /// In case of Hibernate errors
- public int Delete(string queryString, object value, IType type)
- {
- return Delete(queryString, new Object[] {value}, new IType[] {type});
- }
-
- ///
- /// Delete all objects returned by the query.
- ///
- /// a query expressed in Hibernate's query language.
- /// The values of the parameters.
- /// Hibernate types of the parameters (or null)
- /// The number of entity instances deleted.
- /// In case of Hibernate errors
- /// If length for argument values and types are not equal.
- public int Delete(String queryString, Object[] values, IType[] types)
- {
- if (values != null && types != null && values.Length != types.Length)
- {
- throw new ArgumentOutOfRangeException("values", "Length of values array must match length of types array");
- }
- return (int)Execute(new DeletebyQueryHibernateCallback(this, queryString, values, types),true);
-
- }
-
-
- ///
- /// Delete all given persistent instances.
- ///
- /// The persistent instances to delete.
- ///
- /// This can be combined with any of the find methods to delete by query
- /// in two lines of code, similar to Session's delete by query methods.
- ///
- /// In case of Hibernate errors
- public void DeleteAll(ICollection entities)
- {
- Execute(new DeleteAllHibernateCallback(this, entities),true);
- }
-
-
- ///
- /// Execute the action specified by the given action object within a Session.
- ///
- ///
- /// Application exceptions thrown by the action object get propagated to the
- /// caller (can only be unchecked). Hibernate exceptions are transformed into
- /// appropriate DAO ones. Allows for returning a result object, i.e. a domain
- /// object or a collection of domain objects.
- ///
Note: Callback code is not supposed to handle transactions itself!
- /// Use an appropriate transaction manager like HibernateTransactionManager.
- /// Generally, callback code must not touch any Session lifecycle methods,
- /// like close, disconnect, or reconnect, to let the template do its work.
- ///
- ///
- /// The delegate callback object that specifies the Hibernate action.
- /// a result object returned by the action, or null
- ///
- /// In case of Hibernate errors
- public object Execute(HibernateDelegate del)
- {
- return Execute(new ExecuteHibernateCallbackUsingDelegate(del));
- }
-
- ///
- /// Execute the action specified by the delegate within a Session.
- ///
- /// The HibernateDelegate that specifies the action
- /// to perform.
- /// if set to true expose the native hibernate session to
- /// callback code.
- /// a result object returned by the action, or null
- ///
- public object Execute(HibernateDelegate del, bool exposeNativeSession)
- {
- return Execute(new ExecuteHibernateCallbackUsingDelegate(del), true);
- }
-
- ///
- /// Execute the action specified by the given action object within a Session.
- ///
- /// The callback object that specifies the Hibernate action.
- ///
- /// a result object returned by the action, or null
- ///
- ///
- /// Application exceptions thrown by the action object get propagated to the
- /// caller (can only be unchecked). Hibernate exceptions are transformed into
- /// appropriate DAO ones. Allows for returning a result object, i.e. a domain
- /// object or a collection of domain objects.
- ///
Note: Callback code is not supposed to handle transactions itself!
- /// Use an appropriate transaction manager like HibernateTransactionManager.
- /// Generally, callback code must not touch any Session lifecycle methods,
- /// like close, disconnect, or reconnect, to let the template do its work.
- ///
- ///
- /// In case of Hibernate errors
- public object Execute(IHibernateCallback action)
- {
- return Execute(action, ExposeNativeSession);
- }
-
- ///
- /// Execute the specified action assuming that the result object is a List.
- ///
- ///
- /// This is a convenience method for executing Hibernate find calls or
- /// queries within an action.
- ///
- /// The calback object that specifies the Hibernate action.
- /// A IList returned by the action, or null
- ///
- /// In case of Hibernate errors
- public IList ExecuteFind(IHibernateCallback action)
- {
- Object result = Execute(action, ExposeNativeSession);
- if (result != null && !(result is IList)) {
- throw new InvalidDataAccessApiUsageException(
- "Result object returned from HibernateCallback isn't a List: [" + result + "]");
- }
- return (IList) result;
- }
-
- ///
- /// Execute the action specified by the given action object within a Session.
- ///
- /// callback object that specifies the Hibernate action.
- /// if set to true expose the native hibernate session to
- /// callback code.
- ///
- /// a result object returned by the action, or null
- ///
- public object Execute(IHibernateCallback action, bool exposeNativeSession)
- {
- ISession session = Session;
-
- bool existingTransaction = SessionFactoryUtils.IsSessionTransactional(session, SessionFactory);
- if (existingTransaction)
- {
- if(log.IsDebugEnabled) log.Debug("Found thread-bound Session for HibernateTemplate");
- }
-
- FlushModeHolder previousFlushModeHolder = new FlushModeHolder();
- try
- {
- previousFlushModeHolder = ApplyFlushMode(session, existingTransaction);
- ISession sessionToExpose = (exposeNativeSession ? session : CreateSessionProxy(session));
- Object result = action.DoInHibernate(sessionToExpose);
- FlushIfNecessary(session, existingTransaction);
- return result;
- }
- catch (ADOException ex)
- {
- IDbProvider dbProvider = SessionFactoryUtils.GetDbProvider(SessionFactory);
- if (dbProvider != null && dbProvider.IsDataAccessException(ex.InnerException))
- {
- throw ConvertAdoAccessException(ex);
- }
- else
- {
- throw new HibernateSystemException(ex);
- }
- }
- catch (HibernateException ex)
- {
- throw ConvertHibernateAccessException(ex);
- }
- catch (Exception ex)
- {
- IDbProvider dbProvider = SessionFactoryUtils.GetDbProvider(SessionFactory);
- if (dbProvider != null && dbProvider.IsDataAccessException(ex))
- {
- throw ConvertAdoAccessException(ex);
- }
- else
- {
- // Callback code throw application exception or other non DB related exception.
- throw;
- }
- }
- finally
- {
- if (existingTransaction)
- {
- if (log.IsDebugEnabled) log.Debug("Not closing pre-bound Hibernate Session after HibernateTemplate");
- if (previousFlushModeHolder.ModeWasSet)
- {
- session.FlushMode = previousFlushModeHolder.Mode;
- }
- }
- else
- {
- // Never use deferred close for an explicitly new Session.
- if (AlwaysUseNewSession)
- {
- SessionFactoryUtils.CloseSession(session);
- }
- else
- {
- SessionFactoryUtils.CloseSessionOrRegisterDeferredClose(session, SessionFactory);
- }
- }
- }
- }
-
- ///
- /// Execute a query for persistent instances.
- ///
- /// a query expressed in Hibernate's query language
- ///
- /// a List containing 0 or more persistent instances
- ///
- /// In case of Hibernate errors
- public IList Find(string queryString)
- {
- return Find(queryString, (object[])null, (IType[])null );
- }
-
- ///
- /// Execute a query for persistent instances, binding
- /// one value to a "?" parameter in the query string.
- ///
- /// a query expressed in Hibernate's query language
- /// the value of the parameter
- ///
- /// a List containing 0 or more persistent instances
- ///
- /// In case of Hibernate errors
- public IList Find(string queryString, object value)
- {
- return Find(queryString, new object[] {value}, (IType[]) null);
- }
-
- ///
- /// Execute a query for persistent instances, binding one value
- /// to a "?" parameter of the given type in the query string.
- ///
- /// a query expressed in Hibernate's query language
- /// The value of the parameter.
- /// Hibernate type of the parameter (or null)
- ///
- /// a List containing 0 or more persistent instances
- ///
- /// In case of Hibernate errors
- public IList Find(string queryString, object value, IType type)
- {
- return Find(queryString, new object[] {value}, new IType[] {type});
- }
-
- ///
- /// Execute a query for persistent instances, binding a
- /// number of values to "?" parameters in the query string.
- ///
- /// a query expressed in Hibernate's query language
- /// the values of the parameters
- /// a List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList Find(string queryString, object[] values)
- {
- return Find(queryString, values, (IType[]) null);
- }
-
- ///
- /// Execute a query for persistent instances, binding a number of
- /// values to "?" parameters of the given types in the query string.
- ///
- /// A query expressed in Hibernate's query language
- /// The values of the parameters
- /// Hibernate types of the parameters (or null)
- ///
- /// a List containing 0 or more persistent instances
- ///
- /// In case of Hibernate errors
- /// If values and types are not null and their lengths are not equal
- public IList Find(string queryString, object[] values, IType[] types)
- {
- if (values != null && types != null && values.Length != types.Length)
- {
- throw new ArgumentException("Length of values array must match length of types array");
- }
- return (IList)Execute(new FindHibernateCallback(this, queryString, values, types),true);
- }
-
- ///
- /// Execute a query for persistent instances, binding
- /// one value to a named parameter in the query string.
- ///
- /// The name of a Hibernate query in a mapping file
- /// The name of the parameter
- /// The value of the parameter
- /// a List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByNamedParam(string queryName, string paramName, object value)
- {
- return FindByNamedParam(queryName, paramName, value, null);
- }
-
- ///
- /// Execute a query for persistent instances, binding
- /// one value to a named parameter in the query string.
- ///
- /// The name of a Hibernate query in a mapping file
- /// The name of the parameter
- /// The value of the parameter
- /// Hibernate type of the parameter (or null)
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByNamedParam(string queryName, string paramName, object value, IType type)
- {
- return FindByNamedParam(queryName, new string[] {paramName}, new object[] {value}, new IType[] {type});
- }
-
- ///
- /// Execute a query for persistent instances, binding a
- /// number of values to named parameters in the query string.
- ///
- /// A query expressed in Hibernate's query language
- /// The names of the parameters
- /// The values of the parameters
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByNamedParam(string queryString, string[] paramNames, object[] values)
- {
- return FindByNamedParam(queryString, paramNames, values, null);
- }
-
- ///
- /// Execute a query for persistent instances, binding a
- /// number of values to named parameters in the query string.
- ///
- /// A query expressed in Hibernate's query language
- /// The names of the parameters
- /// The values of the parameters
- /// Hibernate types of the parameters (or null)
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- /// If paramNames length is not equal to values length or
- /// if paramNames length is not equal to types length (when types is not null)
- public IList FindByNamedParam(string queryString, string[] paramNames, object[] values, IType[] types)
- {
- if (paramNames.Length != values.Length)
- {
- throw new ArgumentOutOfRangeException("paramNames","Length of paramNames array must match length of values array");
- }
- if (types != null && paramNames.Length != types.Length)
- {
- throw new ArgumentOutOfRangeException("paramNames","Length of paramNames array must match length of types array");
- }
-
- return (IList)Execute(new FindByNamedParamHibernateCallback(this, queryString, paramNames, values, types),true);
-
- }
-
- ///
- /// Execute a named query for persistent instances.
- /// A named query is defined in a Hibernate mapping file.
- ///
- /// The name of a Hibernate query in a mapping file
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByNamedQuery(string queryName)
- {
- return FindByNamedQuery(queryName, (object[]) null, (IType[]) null);
- }
-
- ///
- /// Execute a named query for persistent instances, binding
- /// one value to a "?" parameter in the query string.
- /// A named query is defined in a Hibernate mapping file.
- ///
- /// The name of a Hibernate query in a mapping file
- /// The value of the parameter
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByNamedQuery(string queryName, object value)
- {
- return FindByNamedQuery(queryName, new object[] {value}, (IType[]) null);
- }
-
- ///
- /// Execute a named query for persistent instances, binding
- /// one value to a "?" parameter in the query string.
- /// A named query is defined in a Hibernate mapping file.
- ///
- /// The name of a Hibernate query in a mapping file
- /// The value of the parameter
- /// Hibernate type of the parameter (or null)
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByNamedQuery(string queryName, object value, IType type)
- {
- return FindByNamedQuery(queryName, new object[] { value }, new IType[] { type });
- }
-
- ///
- /// Execute a named query for persistent instances, binding a
- /// number of values to "?" parameters in the query string.
- /// A named query is defined in a Hibernate mapping file.
- ///
- /// The name of a Hibernate query in a mapping file
- /// The values of the parameters
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByNamedQuery(string queryName, object[] values)
- {
- return FindByNamedQuery(queryName, values, (IType[]) null);
- }
-
- ///
- /// Execute a named query for persistent instances, binding a
- /// number of values to "?" parameters in the query string.
- /// A named query is defined in a Hibernate mapping file.
- ///
- /// The name of a Hibernate query in a mapping file
- /// The values of the parameters
- /// Hibernate types of the parameters (or null)
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- /// If values and types are not null and their lengths differ.
- public IList FindByNamedQuery(string queryName, object[] values, IType[] types)
- {
- if (values != null && types != null && values.Length != types.Length)
- {
- throw new ArgumentOutOfRangeException("Length of values array must match length of types array");
- }
- return (IList)Execute(new FindByNamedQueryHibernateCallback(this, queryName, values, types),true);
-
-
- }
-
- ///
- /// Execute a named query for persistent instances, binding
- /// one value to a named parameter in the query string.
- /// A named query is defined in a Hibernate mapping file.
- ///
- /// The name of a Hibernate query in a mapping file
- /// Name of the parameter
- /// The value of the parameter
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByNamedQueryAndNamedParam(string queryName, string paramName, object value)
- {
- return FindByNamedQueryAndNamedParam(queryName, paramName, value, null);
-
- }
-
- ///
- /// Execute a named query for persistent instances, binding
- /// one value to a named parameter in the query string.
- /// A named query is defined in a Hibernate mapping file.
- ///
- /// The name of a Hibernate query in a mapping file
- /// Name of the parameter
- /// The value of the parameter
- /// The Hibernate type of the parameter (or null)
- /// A List containing 0 or more persistent instances
- public IList FindByNamedQueryAndNamedParam(string queryName, string paramName, object value, IType type)
- {
- return FindByNamedQueryAndNamedParam(
- queryName, new string[] {paramName}, new object[] {value}, new IType[] {type});
-
- }
-
- ///
- /// Execute a named query for persistent instances, binding
- /// number of values to named parameters in the query string.
- /// A named query is defined in a Hibernate mapping file.
- ///
- /// The name of a Hibernate query in a mapping file
- /// The names of the parameters
- /// The values of the parameters.
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByNamedQueryAndNamedParam(string queryName, string[] paramNames, object[] values)
- {
- return FindByNamedQueryAndNamedParam(queryName, paramNames, values, null);
-
- }
-
- ///
- /// Execute a named query for persistent instances, binding
- /// number of values to named parameters in the query string.
- /// A named query is defined in a Hibernate mapping file.
- ///
- /// The name of a Hibernate query in a mapping file
- /// The names of the parameters
- /// The values of the parameters.
- /// Hibernate types of the parameters (or null)
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- /// If paramNames length is not equal to values length or
- /// if paramNames length is not equal to types length (when types is not null)
- public IList FindByNamedQueryAndNamedParam(string queryName, string[] paramNames, object[] values, IType[] types)
- {
- if (paramNames != null && values != null && paramNames.Length != values.Length)
- {
- throw new ArgumentOutOfRangeException("paramNames","Length of paramNames array must match length of values array");
- }
- if (paramNames != null && types != null && paramNames.Length != types.Length)
- {
- throw new ArgumentOutOfRangeException("paramNams","Length of paramNames array must match length of types array");
- }
- return (IList)Execute(new FindByNamedQueryAndNamedParamHibernateCallback(this, queryName, paramNames, values, types),true);
-
- }
-
- ///
- /// Execute a named query for persistent instances, binding the properties
- /// of the given object to named parameters in the query string.
- /// A named query is defined in a Hibernate mapping file.
- ///
- /// The name of a Hibernate query in a mapping file
- /// The values of the parameters
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByNamedQueryAndValueObject(string queryName, object valueObject)
- {
- return (IList)Execute(new FindByNamedQueryAndValueObjectHibernateCallback(this, queryName, valueObject),true);
-
- }
-
- ///
- /// Execute a query for persistent instances, binding the properties
- /// of the given object to named parameters in the query string.
- ///
- /// A query expressed in Hibernate's query language
- /// The values of the parameters
- /// A List containing 0 or more persistent instances
- /// In case of Hibernate errors
- public IList FindByValueObject(string queryString, object valueObject)
- {
- return (IList)Execute(new FindByValueObjectHibernateCallback(this, queryString, valueObject), true);
- }
-
- #endregion
-
- #region Methods
-
-
- ///
- /// Create a close-suppressing proxy for the given Hibernate Session.
- /// The proxy also prepares returned Query and Criteria objects.
- ///
- /// The session.
- /// The session proxy.
- public virtual ISession CreateSessionProxy(ISession session)
- {
- //TODO can move to HibernateAccessor and make protected
- // if issue reported with AOP+Multiple Threads resolve.
- // have not been able to reproduce so added lock as a precaution.
- //
- lock (syncRoot)
- {
- if (sessionProxyFactory == null)
- {
- sessionProxyFactory = new ProxyFactory();
- sessionProxyFactory.AddAdvice(new CloseSuppressingMethodInterceptor(this));
- }
-
- sessionProxyFactory.Target = session;
-
- return (ISession)sessionProxyFactory.GetProxy();
- }
- }
-
- ///
- /// Check whether write operations are allowed on the given Session.
- ///
- ///
- /// Default implementation throws an InvalidDataAccessApiUsageException
- /// in case of FlushMode.Never. Can be overridden in subclasses.
- ///
- /// The current Hibernate session.
- /// If write operation is attempted in read-only mode
- ///
- public virtual void CheckWriteOperationAllowed(ISession session)
- {
- if (CheckWriteOperations && TemplateFlushMode != TemplateFlushMode.Eager &&
- AreEqualFlushMode(TemplateFlushMode.Never, session.FlushMode))
- {
- throw new InvalidDataAccessApiUsageException(
- "Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session " +
- "into FlushMode.AUTO or remove 'readOnly' marker from transaction definition");
- }
- }
-
-
-
- ///
- /// Compares if the flush mode enumerations, Spring's
- /// TemplateFlushMode and NHibernates FlushMode have equal
- /// settings.
- ///
- /// The template flush mode.
- /// The NHibernate flush mode.
- ///
- /// Returns true if both are Never, Auto, or Commit, false
- /// otherwise.
- ///
- protected bool AreEqualFlushMode(TemplateFlushMode tfm, FlushMode fm)
- {
- if ( (tfm ==TemplateFlushMode.Never && fm == FlushMode.Never) ||
- (tfm ==TemplateFlushMode.Auto && fm == FlushMode.Auto) ||
- (tfm ==TemplateFlushMode.Commit && fm == FlushMode.Commit) )
- {
- return true;
- }
- else
- {
- return false;
- }
- //TODO other combinations.
- }
-
- #endregion
- }
-
- #region Internal Supporting Callback Classes
-
- //TODO see if can create common base class for some callbacks.
-
- internal class ContainsHibernateCallback : IHibernateCallback
- {
- private object entity;
- public ContainsHibernateCallback(object entity)
- {
- this.entity = entity;
- }
-
- public object DoInHibernate(ISession session)
- {
- return session.Contains(entity);
- }
- }
-
-
- internal class DeleteLockModeHibernateCallback : IHibernateCallback
- {
- private HibernateTemplate outer;
- private object entity;
- private LockMode lockMode;
- public DeleteLockModeHibernateCallback(HibernateTemplate template, object entity, LockMode lockMode)
- {
- this.outer = template;
- this.entity = entity;
- this.lockMode = lockMode;
- }
-
- public object DoInHibernate(ISession session)
- {
- outer.CheckWriteOperationAllowed(session);
- if (lockMode != null)
- {
- session.Lock(entity, lockMode);
- }
- session.Delete(entity);
- return null;
- }
- }
-
-
- internal class DeletebyQueryHibernateCallback : IHibernateCallback
- {
- private HibernateTemplate outer;
- private string queryString;
- private object[] values;
- private IType[] types;
-
- public DeletebyQueryHibernateCallback(HibernateTemplate template, string queryString, object[] values, IType[] types)
- {
- this.outer = template;
- this.queryString = queryString;
- this.values = values;
- this.types = types;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- outer.CheckWriteOperationAllowed(session);
- if (values != null)
- {
- return session.Delete(queryString, values, types);
- }
- else
- {
- return session.Delete(queryString);
- }
- }
- }
-
-
-
- internal class DeleteAllHibernateCallback : IHibernateCallback
- {
- HibernateTemplate outer;
- private ICollection entities;
-
- public DeleteAllHibernateCallback(HibernateTemplate template, ICollection entities)
- {
- this.outer = template;
- this.entities = entities;
-
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- outer.CheckWriteOperationAllowed(session);
- foreach (object entity in entities)
- {
- session.Delete(entity);
- }
- return null;
- }
-
-
-
- }
-
-
- internal class EvictHibernateCallback : IHibernateCallback
- {
- private object entity;
-
- public EvictHibernateCallback(object entity)
- {
- this.entity = entity;
-
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- session.Evict(entity);
- return null;
- }
-
-
-
- }
-
-
- internal class FindHibernateCallback : IHibernateCallback
- {
- private HibernateTemplate outer;
- private string queryString;
- private object[] values;
- private IType[] types;
-
- public FindHibernateCallback(HibernateTemplate template, string queryString, object[] values, IType[] types)
- {
- this.outer = template;
- this.queryString = queryString;
- this.values = values;
- this.types = types;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- IQuery queryObject = session.CreateQuery(queryString);
- outer.PrepareQuery(queryObject);
- if (values != null)
- {
- for (int i = 0; i < values.Length; i++)
- {
- if (types != null && types[i] != null)
- {
- queryObject.SetParameter(i, values[i], types[i]);
- }
- else
- {
- queryObject.SetParameter(i, values[i]);
- }
- }
- }
-
- return queryObject.List();
- }
- }
-
-
- internal class FindByNamedParamHibernateCallback : IHibernateCallback
- {
- HibernateTemplate outer;
- private string queryString;
- private string[] paramNames;
- private object[] values;
- private IType[] types;
-
- public FindByNamedParamHibernateCallback(HibernateTemplate template, string queryString, string[] paramNames, object[] values, IType[] types)
- {
- this.outer = template;
- this.queryString = queryString;
- this.paramNames = paramNames;
- this.values = values;
- this.types = types;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- IQuery queryObject = session.CreateQuery(queryString);
- outer.PrepareQuery(queryObject);
- if (values != null)
- {
- for (int i = 0; i < values.Length; i++)
- {
- outer.ApplyNamedParameterToQuery(queryObject, paramNames[i], values[i], (types != null ? types[i] : null));
- }
- }
- return queryObject.List();
-
- }
- }
-
-
- internal class FindByNamedQueryHibernateCallback : IHibernateCallback
- {
- HibernateTemplate outer;
- private string queryName;
- private object[] values;
- private IType[] types;
-
- public FindByNamedQueryHibernateCallback(HibernateTemplate template, string queryName, object[] values, IType[] types)
- {
- this.outer = template;
- this.queryName = queryName;
- this.values = values;
- this.types = types;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- IQuery queryObject = session.GetNamedQuery(queryName);
- outer.PrepareQuery(queryObject);
- if (values != null)
- {
- for (int i = 0; i < values.Length; i++)
- {
- if (types != null && types[i] != null)
- {
- queryObject.SetParameter(i, values[i], types[i]);
- }
- else
- {
- queryObject.SetParameter(i, values[i]);
- }
- }
- }
- return queryObject.List();
-
- }
- }
-
-
- internal class FindByNamedQueryAndNamedParamHibernateCallback : IHibernateCallback
- {
- HibernateTemplate outer;
- private string queryName;
- private string[] paramNames;
- private object[] values;
- private IType[] types;
-
- public FindByNamedQueryAndNamedParamHibernateCallback(HibernateTemplate template, string queryName, string[] paramNames, object[] values, IType[] types)
- {
- this.outer = template;
- this.queryName = queryName;
- this.paramNames = paramNames;
- this.values = values;
- this.types = types;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- IQuery queryObject = session.GetNamedQuery(queryName);
- outer.PrepareQuery(queryObject);
- if (values != null)
- {
- for (int i = 0; i < values.Length; i++)
- {
- outer.ApplyNamedParameterToQuery(queryObject, paramNames[i], values[i], (types != null ? types[i] : null));
- }
- }
- return queryObject.List();
-
- }
- }
-
-
- internal class FindByNamedQueryAndValueObjectHibernateCallback : IHibernateCallback
- {
- HibernateTemplate outer;
- private string queryName;
- private object valueObject;
-
- public FindByNamedQueryAndValueObjectHibernateCallback(HibernateTemplate template, string queryName, object valueObject)
- {
- this.outer = template;
- this.queryName = queryName;
- this.valueObject = valueObject;
-
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- IQuery queryObject = session.GetNamedQuery(queryName);
- outer.PrepareQuery(queryObject);
- queryObject.SetProperties(valueObject);
- return queryObject.List();
-
- }
-
-
-
- }
-
- internal class FindByValueObjectHibernateCallback : IHibernateCallback
- {
- HibernateTemplate outer;
- private string queryString;
- private object valueObject;
-
- public FindByValueObjectHibernateCallback(HibernateTemplate template, string queryString, object valueObject)
- {
- this.outer = template;
- this.queryString = queryString;
- this.valueObject = valueObject;
-
- }
-
- public object DoInHibernate(ISession session)
- {
- IQuery queryObject = session.CreateQuery(queryString);
- outer.PrepareQuery(queryObject);
- queryObject.SetProperties(valueObject);
- return queryObject.List();
-
- }
-
- }
-
- internal class ExecuteHibernateCallbackUsingDelegate : IHibernateCallback
- {
- private HibernateDelegate del;
-
- public ExecuteHibernateCallbackUsingDelegate(HibernateDelegate d)
- {
- del = d;
- }
-
- public object DoInHibernate(ISession session)
- {
- return del(session);
- }
- }
-
-
- internal class GetByTypeHibernateCallback : IHibernateCallback
- {
- private Type entityType;
- private object id;
- private LockMode lockMode;
-
- public GetByTypeHibernateCallback(Type entityType, object id, LockMode lockMode)
- {
- this.entityType = entityType;
- this.id = id;
- this.lockMode = lockMode;
-
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- if (lockMode != null)
- {
- return session.Get(entityType, id, lockMode);
- }
- else
- {
- return session.Get(entityType, id);
- }
- }
-
-
-
- }
-
-
- internal class LoadByTypeHibernateCallback : IHibernateCallback
- {
- private Type entityType;
- private object id;
- private LockMode lockMode;
-
- public LoadByTypeHibernateCallback(Type entityType, object id, LockMode lockMode)
- {
- this.entityType = entityType;
- this.id = id;
- this.lockMode = lockMode;
-
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- if (lockMode != null)
- {
- return session.Load(entityType, id, lockMode);
- }
- else
- {
- return session.Load(entityType, id);
- }
- }
-
-
-
- }
-
-
- internal class LoadByEntityHibernateCallback : IHibernateCallback
- {
- private object entity;
- private object id;
-
- public LoadByEntityHibernateCallback(object entity, object id)
- {
- this.entity = entity;
- this.id = id;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- session.Load(entity, id);
- return null;
- }
-
-
-
- }
-
-
- internal class LoadAllByTypeHibernateCallback : IHibernateCallback
- {
- private HibernateTemplate outer;
- private Type entityType;
-
- public LoadAllByTypeHibernateCallback(HibernateTemplate template, Type entityType)
- {
- outer = template;
- this.entityType = entityType;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- ICriteria criteria = session.CreateCriteria(entityType);
- outer.PrepareCriteria(criteria);
- return criteria.List();
- }
- }
-
-
- internal class LockHibernateCallback : IHibernateCallback
- {
- private object entity;
- private LockMode lockMode;
-
- public LockHibernateCallback(object entity, LockMode lockMode)
- {
- this.entity = entity;
- this.lockMode = lockMode;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- session.Lock(entity, lockMode);
- return null;
- }
- }
-
-
- internal class RefreshHibernateCallback : IHibernateCallback
- {
- private object entity;
- private LockMode lockMode;
-
- public RefreshHibernateCallback(object entity, LockMode lockMode)
- {
- this.entity = entity;
- this.lockMode = lockMode;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- if (lockMode != null)
- {
- session.Refresh(entity, lockMode);
- }
- else
- {
- session.Refresh(entity);
- }
- return null;
- }
- }
-
-
- internal class SaveObjectHibernateCallback : IHibernateCallback
- {
- private HibernateTemplate outer;
- private object entity;
-
- public SaveObjectHibernateCallback(HibernateTemplate template, object entity)
- {
- this.outer = template;
- this.entity = entity;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- outer.CheckWriteOperationAllowed(session);
- return session.Save(entity);
- }
- }
-
-
- internal class SaveObjectWithIdHibernateCallback : IHibernateCallback
- {
- private HibernateTemplate outer;
- private object entity;
- private object id;
-
- public SaveObjectWithIdHibernateCallback(HibernateTemplate template, object entity, object id)
- {
- this.outer = template;
- this.entity = entity;
- this.id = id;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- outer.CheckWriteOperationAllowed(session);
- session.Save(entity, id);
- return null;
- }
- }
-
-
- internal class UpdateObjectHibernateCallback : IHibernateCallback
- {
- private HibernateTemplate outer;
- private object entity;
- private LockMode lockMode;
-
- public UpdateObjectHibernateCallback(HibernateTemplate template, object entity, LockMode lockMode)
- {
- this.outer = template;
- this.entity = entity;
- this.lockMode = lockMode;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- outer.CheckWriteOperationAllowed(session);
- session.Update(entity);
- if (lockMode != null)
- {
- session.Lock(entity, lockMode);
- }
- return null;
- }
- }
-
-
- internal class SaveOrUpdateObjectHibernateCallback : IHibernateCallback
- {
- private HibernateTemplate outer;
- private object entity;
-
- public SaveOrUpdateObjectHibernateCallback(HibernateTemplate template, object entity)
- {
- this.outer = template;
- this.entity = entity;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- outer.CheckWriteOperationAllowed(session);
- session.SaveOrUpdate(entity);
- return null;
- }
- }
-
- internal class SaveOrUpdateAllHibernateCallback : IHibernateCallback
- {
- private HibernateTemplate outer;
- private ICollection entities;
-
- public SaveOrUpdateAllHibernateCallback(HibernateTemplate template, ICollection entities)
- {
- this.outer = template;
- this.entities = entities;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- outer.CheckWriteOperationAllowed(session);
- foreach (object entity in entities)
- {
- session.SaveOrUpdate(entity);
- }
- return null;
- }
- }
- internal class SaveOrUpdateCopyHibernateCallback : IHibernateCallback
- {
- private HibernateTemplate outer;
- private object entity;
-
- public SaveOrUpdateCopyHibernateCallback(HibernateTemplate template, object entity)
- {
- this.outer = template;
- this.entity = entity;
- }
-
- ///
- /// Gets called by HibernateTemplate with an active
- /// Hibernate Session. Does not need to care about activating or closing
- /// the Session, or handling transactions.
- ///
- ///
- ///
- /// Allows for returning a result object created within the callback, i.e.
- /// a domain object or a collection of domain objects. Note that there's
- /// special support for single step actions: see HibernateTemplate.find etc.
- ///
- ///
- public object DoInHibernate(ISession session)
- {
- outer.CheckWriteOperationAllowed(session);
+ }
+#endif
+
+ ///
+ /// Remove all objects from the Session cache, and cancel all pending saves,
+ /// updates and deletes.
+ ///
+ public void Clear()
+ {
+ Execute(new HibernateDelegate(ClearAction), true);
+ }
+
+
+
+ ///
+ /// Determines whether the given object is in the Session cache.
+ ///
+ /// the persistence instance to check.
+ ///
+ /// true if session cache contains the specified entity; otherwise, false.
+ ///
+ /// In case of Hibernate errors
+ public bool Contains(object entity)
+ {
+ return (bool)Execute(new ContainsHibernateCallback(entity));
+ }
+
+ ///
+ /// Remove the given object from the Session cache.
+ ///
+ /// The persistent instance to evict.
+ /// In case of Hibernate errors
+ public void Evict(object entity)
+ {
+ Execute(new EvictHibernateCallback(entity), true);
+
+ }
+
+
+
+ ///
+ /// Delete the given persistent instance.
+ ///
+ /// The persistent instance to delete.
+ /// In case of Hibernate errors
+ public void Delete(object entity)
+ {
+ Delete(entity, null);
+ }
+
+
+ ///
+ /// Delete the given persistent instance.
+ ///
+ /// Tthe persistent instance to delete.
+ /// The lock mode to obtain.
+ ///
+ /// Obtains the specified lock mode if the instance exists, implicitly
+ /// checking whether the corresponding database entry still exists
+ /// (throwing an OptimisticLockingFailureException if not found).
+ ///
+ /// In case of Hibernate errors
+ public void Delete(object entity, LockMode lockMode)
+ {
+ Execute(new DeleteLockModeHibernateCallback(this, entity, lockMode), true);
+ }
+
+ ///
+ /// Delete all objects returned by the query.
+ ///
+ /// a query expressed in Hibernate's query language.
+ /// The number of entity instances deleted.
+ /// In case of Hibernate errors
+ public int Delete(string queryString)
+ {
+ return Delete(queryString, (Object[]) null, (IType[]) null);
+ }
+
+ ///
+ /// Delete all objects returned by the query.
+ ///
+ /// a query expressed in Hibernate's query language.
+ /// The value of the parameter.
+ /// The Hibernate type of the parameter (or null).
+ /// The number of entity instances deleted.
+ /// In case of Hibernate errors
+ public int Delete(string queryString, object value, IType type)
+ {
+ return Delete(queryString, new Object[] {value}, new IType[] {type});
+ }
+
+ ///
+ /// Delete all objects returned by the query.
+ ///
+ /// a query expressed in Hibernate's query language.
+ /// The values of the parameters.
+ /// Hibernate types of the parameters (or null)
+ /// The number of entity instances deleted.
+ /// In case of Hibernate errors
+ /// If length for argument values and types are not equal.
+ public int Delete(String queryString, Object[] values, IType[] types)
+ {
+ if (values != null && types != null && values.Length != types.Length)
+ {
+ throw new ArgumentOutOfRangeException("values", "Length of values array must match length of types array");
+ }
+ return (int)Execute(new DeletebyQueryHibernateCallback(this, queryString, values, types),true);
+
+ }
+
+
+ ///
+ /// Delete all given persistent instances.
+ ///
+ /// The persistent instances to delete.
+ ///
+ /// This can be combined with any of the find methods to delete by query
+ /// in two lines of code, similar to Session's delete by query methods.
+ ///
+ /// In case of Hibernate errors
+ public void DeleteAll(ICollection entities)
+ {
+ Execute(new DeleteAllHibernateCallback(this, entities),true);
+ }
+
+
+ ///
+ /// Execute the action specified by the given action object within a Session.
+ ///
+ ///
+ /// Application exceptions thrown by the action object get propagated to the
+ /// caller (can only be unchecked). Hibernate exceptions are transformed into
+ /// appropriate DAO ones. Allows for returning a result object, i.e. a domain
+ /// object or a collection of domain objects.
+ ///
Note: Callback code is not supposed to handle transactions itself!
+ /// Use an appropriate transaction manager like HibernateTransactionManager.
+ /// Generally, callback code must not touch any Session lifecycle methods,
+ /// like close, disconnect, or reconnect, to let the template do its work.
+ ///
+ ///
+ /// The delegate callback object that specifies the Hibernate action.
+ /// a result object returned by the action, or null
+ ///
+ /// In case of Hibernate errors
+ public object Execute(HibernateDelegate del)
+ {
+ return Execute(new ExecuteHibernateCallbackUsingDelegate(del));
+ }
+
+ ///
+ /// Execute the action specified by the delegate within a Session.
+ ///
+ /// The HibernateDelegate that specifies the action
+ /// to perform.
+ /// if set to true expose the native hibernate session to
+ /// callback code.
+ /// a result object returned by the action, or null
+ ///
+ public object Execute(HibernateDelegate del, bool exposeNativeSession)
+ {
+ return Execute(new ExecuteHibernateCallbackUsingDelegate(del), true);
+ }
+
+ ///
+ /// Execute the action specified by the given action object within a Session.
+ ///
+ /// The callback object that specifies the Hibernate action.
+ ///
+ /// a result object returned by the action, or null
+ ///
+ ///
+ /// Application exceptions thrown by the action object get propagated to the
+ /// caller (can only be unchecked). Hibernate exceptions are transformed into
+ /// appropriate DAO ones. Allows for returning a result object, i.e. a domain
+ /// object or a collection of domain objects.
+ ///
Note: Callback code is not supposed to handle transactions itself!
+ /// Use an appropriate transaction manager like HibernateTransactionManager.
+ /// Generally, callback code must not touch any Session lifecycle methods,
+ /// like close, disconnect, or reconnect, to let the template do its work.
+ ///
+ ///
+ /// In case of Hibernate errors
+ public object Execute(IHibernateCallback action)
+ {
+ return Execute(action, ExposeNativeSession);
+ }
+
+ ///
+ /// Execute the specified action assuming that the result object is a List.
+ ///
+ ///
+ /// This is a convenience method for executing Hibernate find calls or
+ /// queries within an action.
+ ///
+ /// The calback object that specifies the Hibernate action.
+ /// A IList returned by the action, or null
+ ///
+ /// In case of Hibernate errors
+ public IList ExecuteFind(IHibernateCallback action)
+ {
+ Object result = Execute(action, ExposeNativeSession);
+ if (result != null && !(result is IList)) {
+ throw new InvalidDataAccessApiUsageException(
+ "Result object returned from HibernateCallback isn't a List: [" + result + "]");
+ }
+ return (IList) result;
+ }
+
+ ///
+ /// Execute the action specified by the given action object within a Session.
+ ///
+ /// callback object that specifies the Hibernate action.
+ /// if set to true expose the native hibernate session to
+ /// callback code.
+ ///
+ /// a result object returned by the action, or null
+ ///
+ public object Execute(IHibernateCallback action, bool exposeNativeSession)
+ {
+ ISession session = Session;
+
+ bool existingTransaction = SessionFactoryUtils.IsSessionTransactional(session, SessionFactory);
+ if (existingTransaction)
+ {
+ if(log.IsDebugEnabled) log.Debug("Found thread-bound Session for HibernateTemplate");
+ }
+
+ FlushModeHolder previousFlushModeHolder = new FlushModeHolder();
+ try
+ {
+ previousFlushModeHolder = ApplyFlushMode(session, existingTransaction);
+ ISession sessionToExpose = (exposeNativeSession ? session : CreateSessionProxy(session));
+ Object result = action.DoInHibernate(sessionToExpose);
+ FlushIfNecessary(session, existingTransaction);
+ return result;
+ }
+ catch (ADOException ex)
+ {
+ IDbProvider dbProvider = SessionFactoryUtils.GetDbProvider(SessionFactory);
+ if (dbProvider != null && dbProvider.IsDataAccessException(ex.InnerException))
+ {
+ throw ConvertAdoAccessException(ex);
+ }
+ else
+ {
+ throw new HibernateSystemException(ex);
+ }
+ }
+ catch (HibernateException ex)
+ {
+ throw ConvertHibernateAccessException(ex);
+ }
+ catch (Exception ex)
+ {
+ IDbProvider dbProvider = SessionFactoryUtils.GetDbProvider(SessionFactory);
+ if (dbProvider != null && dbProvider.IsDataAccessException(ex))
+ {
+ throw ConvertAdoAccessException(ex);
+ }
+ else
+ {
+ // Callback code throw application exception or other non DB related exception.
+ throw;
+ }
+ }
+ finally
+ {
+ if (existingTransaction)
+ {
+ if (log.IsDebugEnabled) log.Debug("Not closing pre-bound Hibernate Session after HibernateTemplate");
+ if (previousFlushModeHolder.ModeWasSet)
+ {
+ session.FlushMode = previousFlushModeHolder.Mode;
+ }
+ }
+ else
+ {
+ // Never use deferred close for an explicitly new Session.
+ if (AlwaysUseNewSession)
+ {
+ SessionFactoryUtils.CloseSession(session);
+ }
+ else
+ {
+ SessionFactoryUtils.CloseSessionOrRegisterDeferredClose(session, SessionFactory);
+ }
+ }
+ }
+ }
+
+ ///
+ /// Execute a query for persistent instances.
+ ///
+ /// a query expressed in Hibernate's query language
+ ///
+ /// a List containing 0 or more persistent instances
+ ///
+ /// In case of Hibernate errors
+ public IList Find(string queryString)
+ {
+ return Find(queryString, (object[])null, (IType[])null );
+ }
+
+ ///
+ /// Execute a query for persistent instances, binding
+ /// one value to a "?" parameter in the query string.
+ ///
+ /// a query expressed in Hibernate's query language
+ /// the value of the parameter
+ ///
+ /// a List containing 0 or more persistent instances
+ ///
+ /// In case of Hibernate errors
+ public IList Find(string queryString, object value)
+ {
+ return Find(queryString, new object[] {value}, (IType[]) null);
+ }
+
+ ///
+ /// Execute a query for persistent instances, binding one value
+ /// to a "?" parameter of the given type in the query string.
+ ///
+ /// a query expressed in Hibernate's query language
+ /// The value of the parameter.
+ /// Hibernate type of the parameter (or null)
+ ///
+ /// a List containing 0 or more persistent instances
+ ///
+ /// In case of Hibernate errors
+ public IList Find(string queryString, object value, IType type)
+ {
+ return Find(queryString, new object[] {value}, new IType[] {type});
+ }
+
+ ///
+ /// Execute a query for persistent instances, binding a
+ /// number of values to "?" parameters in the query string.
+ ///
+ /// a query expressed in Hibernate's query language
+ /// the values of the parameters
+ /// a List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList Find(string queryString, object[] values)
+ {
+ return Find(queryString, values, (IType[]) null);
+ }
+
+ ///
+ /// Execute a query for persistent instances, binding a number of
+ /// values to "?" parameters of the given types in the query string.
+ ///
+ /// A query expressed in Hibernate's query language
+ /// The values of the parameters
+ /// Hibernate types of the parameters (or null)
+ ///
+ /// a List containing 0 or more persistent instances
+ ///
+ /// In case of Hibernate errors
+ /// If values and types are not null and their lengths are not equal
+ public IList Find(string queryString, object[] values, IType[] types)
+ {
+ if (values != null && types != null && values.Length != types.Length)
+ {
+ throw new ArgumentException("Length of values array must match length of types array");
+ }
+ return (IList)Execute(new FindHibernateCallback(this, queryString, values, types),true);
+ }
+
+ ///
+ /// Execute a query for persistent instances, binding
+ /// one value to a named parameter in the query string.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// The name of the parameter
+ /// The value of the parameter
+ /// a List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByNamedParam(string queryName, string paramName, object value)
+ {
+ return FindByNamedParam(queryName, paramName, value, null);
+ }
+
+ ///
+ /// Execute a query for persistent instances, binding
+ /// one value to a named parameter in the query string.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// The name of the parameter
+ /// The value of the parameter
+ /// Hibernate type of the parameter (or null)
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByNamedParam(string queryName, string paramName, object value, IType type)
+ {
+ return FindByNamedParam(queryName, new string[] {paramName}, new object[] {value}, new IType[] {type});
+ }
+
+ ///
+ /// Execute a query for persistent instances, binding a
+ /// number of values to named parameters in the query string.
+ ///
+ /// A query expressed in Hibernate's query language
+ /// The names of the parameters
+ /// The values of the parameters
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByNamedParam(string queryString, string[] paramNames, object[] values)
+ {
+ return FindByNamedParam(queryString, paramNames, values, null);
+ }
+
+ ///
+ /// Execute a query for persistent instances, binding a
+ /// number of values to named parameters in the query string.
+ ///
+ /// A query expressed in Hibernate's query language
+ /// The names of the parameters
+ /// The values of the parameters
+ /// Hibernate types of the parameters (or null)
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ /// If paramNames length is not equal to values length or
+ /// if paramNames length is not equal to types length (when types is not null)
+ public IList FindByNamedParam(string queryString, string[] paramNames, object[] values, IType[] types)
+ {
+ if (paramNames.Length != values.Length)
+ {
+ throw new ArgumentOutOfRangeException("paramNames","Length of paramNames array must match length of values array");
+ }
+ if (types != null && paramNames.Length != types.Length)
+ {
+ throw new ArgumentOutOfRangeException("paramNames","Length of paramNames array must match length of types array");
+ }
+
+ return (IList)Execute(new FindByNamedParamHibernateCallback(this, queryString, paramNames, values, types),true);
+
+ }
+
+ ///
+ /// Execute a named query for persistent instances.
+ /// A named query is defined in a Hibernate mapping file.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByNamedQuery(string queryName)
+ {
+ return FindByNamedQuery(queryName, (object[]) null, (IType[]) null);
+ }
+
+ ///
+ /// Execute a named query for persistent instances, binding
+ /// one value to a "?" parameter in the query string.
+ /// A named query is defined in a Hibernate mapping file.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// The value of the parameter
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByNamedQuery(string queryName, object value)
+ {
+ return FindByNamedQuery(queryName, new object[] {value}, (IType[]) null);
+ }
+
+ ///
+ /// Execute a named query for persistent instances, binding
+ /// one value to a "?" parameter in the query string.
+ /// A named query is defined in a Hibernate mapping file.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// The value of the parameter
+ /// Hibernate type of the parameter (or null)
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByNamedQuery(string queryName, object value, IType type)
+ {
+ return FindByNamedQuery(queryName, new object[] { value }, new IType[] { type });
+ }
+
+ ///
+ /// Execute a named query for persistent instances, binding a
+ /// number of values to "?" parameters in the query string.
+ /// A named query is defined in a Hibernate mapping file.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// The values of the parameters
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByNamedQuery(string queryName, object[] values)
+ {
+ return FindByNamedQuery(queryName, values, (IType[]) null);
+ }
+
+ ///
+ /// Execute a named query for persistent instances, binding a
+ /// number of values to "?" parameters in the query string.
+ /// A named query is defined in a Hibernate mapping file.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// The values of the parameters
+ /// Hibernate types of the parameters (or null)
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ /// If values and types are not null and their lengths differ.
+ public IList FindByNamedQuery(string queryName, object[] values, IType[] types)
+ {
+ if (values != null && types != null && values.Length != types.Length)
+ {
+ throw new ArgumentOutOfRangeException("Length of values array must match length of types array");
+ }
+ return (IList)Execute(new FindByNamedQueryHibernateCallback(this, queryName, values, types),true);
+
+
+ }
+
+ ///
+ /// Execute a named query for persistent instances, binding
+ /// one value to a named parameter in the query string.
+ /// A named query is defined in a Hibernate mapping file.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// Name of the parameter
+ /// The value of the parameter
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByNamedQueryAndNamedParam(string queryName, string paramName, object value)
+ {
+ return FindByNamedQueryAndNamedParam(queryName, paramName, value, null);
+
+ }
+
+ ///
+ /// Execute a named query for persistent instances, binding
+ /// one value to a named parameter in the query string.
+ /// A named query is defined in a Hibernate mapping file.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// Name of the parameter
+ /// The value of the parameter
+ /// The Hibernate type of the parameter (or null)
+ /// A List containing 0 or more persistent instances
+ public IList FindByNamedQueryAndNamedParam(string queryName, string paramName, object value, IType type)
+ {
+ return FindByNamedQueryAndNamedParam(
+ queryName, new string[] {paramName}, new object[] {value}, new IType[] {type});
+
+ }
+
+ ///
+ /// Execute a named query for persistent instances, binding
+ /// number of values to named parameters in the query string.
+ /// A named query is defined in a Hibernate mapping file.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// The names of the parameters
+ /// The values of the parameters.
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByNamedQueryAndNamedParam(string queryName, string[] paramNames, object[] values)
+ {
+ return FindByNamedQueryAndNamedParam(queryName, paramNames, values, null);
+
+ }
+
+ ///
+ /// Execute a named query for persistent instances, binding
+ /// number of values to named parameters in the query string.
+ /// A named query is defined in a Hibernate mapping file.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// The names of the parameters
+ /// The values of the parameters.
+ /// Hibernate types of the parameters (or null)
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ /// If paramNames length is not equal to values length or
+ /// if paramNames length is not equal to types length (when types is not null)
+ public IList FindByNamedQueryAndNamedParam(string queryName, string[] paramNames, object[] values, IType[] types)
+ {
+ if (paramNames != null && values != null && paramNames.Length != values.Length)
+ {
+ throw new ArgumentOutOfRangeException("paramNames","Length of paramNames array must match length of values array");
+ }
+ if (paramNames != null && types != null && paramNames.Length != types.Length)
+ {
+ throw new ArgumentOutOfRangeException("paramNams","Length of paramNames array must match length of types array");
+ }
+ return (IList)Execute(new FindByNamedQueryAndNamedParamHibernateCallback(this, queryName, paramNames, values, types),true);
+
+ }
+
+ ///
+ /// Execute a named query for persistent instances, binding the properties
+ /// of the given object to named parameters in the query string.
+ /// A named query is defined in a Hibernate mapping file.
+ ///
+ /// The name of a Hibernate query in a mapping file
+ /// The values of the parameters
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByNamedQueryAndValueObject(string queryName, object valueObject)
+ {
+ return (IList)Execute(new FindByNamedQueryAndValueObjectHibernateCallback(this, queryName, valueObject),true);
+
+ }
+
+ ///
+ /// Execute a query for persistent instances, binding the properties
+ /// of the given object to named parameters in the query string.
+ ///
+ /// A query expressed in Hibernate's query language
+ /// The values of the parameters
+ /// A List containing 0 or more persistent instances
+ /// In case of Hibernate errors
+ public IList FindByValueObject(string queryString, object valueObject)
+ {
+ return (IList)Execute(new FindByValueObjectHibernateCallback(this, queryString, valueObject), true);
+ }
+
+ #endregion
+
+ #region Methods
+
+
+ ///
+ /// Create a close-suppressing proxy for the given Hibernate Session.
+ /// The proxy also prepares returned Query and Criteria objects.
+ ///
+ /// The session.
+ /// The session proxy.
+ public virtual ISession CreateSessionProxy(ISession session)
+ {
+ //TODO can move to HibernateAccessor and make protected
+ // if issue reported with AOP+Multiple Threads resolve.
+ // have not been able to reproduce so added lock as a precaution.
+ //
+ lock (syncRoot)
+ {
+ if (sessionProxyFactory == null)
+ {
+ sessionProxyFactory = new ProxyFactory();
+ sessionProxyFactory.AddAdvice(new CloseSuppressingMethodInterceptor(this));
+ }
+
+ sessionProxyFactory.Target = session;
+
+ return (ISession)sessionProxyFactory.GetProxy();
+ }
+ }
+
+ ///
+ /// Check whether write operations are allowed on the given Session.
+ ///
+ ///
+ /// Default implementation throws an InvalidDataAccessApiUsageException
+ /// in case of FlushMode.Never. Can be overridden in subclasses.
+ ///
+ /// The current Hibernate session.
+ /// If write operation is attempted in read-only mode
+ ///
+ public virtual void CheckWriteOperationAllowed(ISession session)
+ {
+ if (CheckWriteOperations && TemplateFlushMode != TemplateFlushMode.Eager &&
+ AreEqualFlushMode(TemplateFlushMode.Never, session.FlushMode))
+ {
+ throw new InvalidDataAccessApiUsageException(
+ "Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session " +
+ "into FlushMode.AUTO or remove 'readOnly' marker from transaction definition");
+ }
+ }
+
+
+
+ ///
+ /// Compares if the flush mode enumerations, Spring's
+ /// TemplateFlushMode and NHibernates FlushMode have equal
+ /// settings.
+ ///
+ /// The template flush mode.
+ /// The NHibernate flush mode.
+ ///
+ /// Returns true if both are Never, Auto, or Commit, false
+ /// otherwise.
+ ///
+ protected bool AreEqualFlushMode(TemplateFlushMode tfm, FlushMode fm)
+ {
+ if ( (tfm ==TemplateFlushMode.Never && fm == FlushMode.Never) ||
+ (tfm ==TemplateFlushMode.Auto && fm == FlushMode.Auto) ||
+ (tfm ==TemplateFlushMode.Commit && fm == FlushMode.Commit) )
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ //TODO other combinations.
+ }
+
+ #endregion
+ }
+
+ #region Internal Supporting Callback Classes
+
+ //TODO see if can create common base class for some callbacks.
+
+ internal class ContainsHibernateCallback : IHibernateCallback
+ {
+ private object entity;
+ public ContainsHibernateCallback(object entity)
+ {
+ this.entity = entity;
+ }
+
+ public object DoInHibernate(ISession session)
+ {
+ return session.Contains(entity);
+ }
+ }
+
+
+ internal class DeleteLockModeHibernateCallback : IHibernateCallback
+ {
+ private HibernateTemplate outer;
+ private object entity;
+ private LockMode lockMode;
+ public DeleteLockModeHibernateCallback(HibernateTemplate template, object entity, LockMode lockMode)
+ {
+ this.outer = template;
+ this.entity = entity;
+ this.lockMode = lockMode;
+ }
+
+ public object DoInHibernate(ISession session)
+ {
+ outer.CheckWriteOperationAllowed(session);
+ if (lockMode != null)
+ {
+ session.Lock(entity, lockMode);
+ }
+ session.Delete(entity);
+ return null;
+ }
+ }
+
+
+ internal class DeletebyQueryHibernateCallback : IHibernateCallback
+ {
+ private HibernateTemplate outer;
+ private string queryString;
+ private object[] values;
+ private IType[] types;
+
+ public DeletebyQueryHibernateCallback(HibernateTemplate template, string queryString, object[] values, IType[] types)
+ {
+ this.outer = template;
+ this.queryString = queryString;
+ this.values = values;
+ this.types = types;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ outer.CheckWriteOperationAllowed(session);
+ if (values != null)
+ {
+ return session.Delete(queryString, values, types);
+ }
+ else
+ {
+ return session.Delete(queryString);
+ }
+ }
+ }
+
+
+
+ internal class DeleteAllHibernateCallback : IHibernateCallback
+ {
+ HibernateTemplate outer;
+ private ICollection entities;
+
+ public DeleteAllHibernateCallback(HibernateTemplate template, ICollection entities)
+ {
+ this.outer = template;
+ this.entities = entities;
+
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ outer.CheckWriteOperationAllowed(session);
+ foreach (object entity in entities)
+ {
+ session.Delete(entity);
+ }
+ return null;
+ }
+
+
+
+ }
+
+
+ internal class EvictHibernateCallback : IHibernateCallback
+ {
+ private object entity;
+
+ public EvictHibernateCallback(object entity)
+ {
+ this.entity = entity;
+
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ session.Evict(entity);
+ return null;
+ }
+
+
+
+ }
+
+
+ internal class FindHibernateCallback : IHibernateCallback
+ {
+ private HibernateTemplate outer;
+ private string queryString;
+ private object[] values;
+ private IType[] types;
+
+ public FindHibernateCallback(HibernateTemplate template, string queryString, object[] values, IType[] types)
+ {
+ this.outer = template;
+ this.queryString = queryString;
+ this.values = values;
+ this.types = types;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ IQuery queryObject = session.CreateQuery(queryString);
+ outer.PrepareQuery(queryObject);
+ if (values != null)
+ {
+ for (int i = 0; i < values.Length; i++)
+ {
+ if (types != null && types[i] != null)
+ {
+ queryObject.SetParameter(i, values[i], types[i]);
+ }
+ else
+ {
+ queryObject.SetParameter(i, values[i]);
+ }
+ }
+ }
+
+ return queryObject.List();
+ }
+ }
+
+
+ internal class FindByNamedParamHibernateCallback : IHibernateCallback
+ {
+ HibernateTemplate outer;
+ private string queryString;
+ private string[] paramNames;
+ private object[] values;
+ private IType[] types;
+
+ public FindByNamedParamHibernateCallback(HibernateTemplate template, string queryString, string[] paramNames, object[] values, IType[] types)
+ {
+ this.outer = template;
+ this.queryString = queryString;
+ this.paramNames = paramNames;
+ this.values = values;
+ this.types = types;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ IQuery queryObject = session.CreateQuery(queryString);
+ outer.PrepareQuery(queryObject);
+ if (values != null)
+ {
+ for (int i = 0; i < values.Length; i++)
+ {
+ outer.ApplyNamedParameterToQuery(queryObject, paramNames[i], values[i], (types != null ? types[i] : null));
+ }
+ }
+ return queryObject.List();
+
+ }
+ }
+
+
+ internal class FindByNamedQueryHibernateCallback : IHibernateCallback
+ {
+ HibernateTemplate outer;
+ private string queryName;
+ private object[] values;
+ private IType[] types;
+
+ public FindByNamedQueryHibernateCallback(HibernateTemplate template, string queryName, object[] values, IType[] types)
+ {
+ this.outer = template;
+ this.queryName = queryName;
+ this.values = values;
+ this.types = types;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ IQuery queryObject = session.GetNamedQuery(queryName);
+ outer.PrepareQuery(queryObject);
+ if (values != null)
+ {
+ for (int i = 0; i < values.Length; i++)
+ {
+ if (types != null && types[i] != null)
+ {
+ queryObject.SetParameter(i, values[i], types[i]);
+ }
+ else
+ {
+ queryObject.SetParameter(i, values[i]);
+ }
+ }
+ }
+ return queryObject.List();
+
+ }
+ }
+
+
+ internal class FindByNamedQueryAndNamedParamHibernateCallback : IHibernateCallback
+ {
+ HibernateTemplate outer;
+ private string queryName;
+ private string[] paramNames;
+ private object[] values;
+ private IType[] types;
+
+ public FindByNamedQueryAndNamedParamHibernateCallback(HibernateTemplate template, string queryName, string[] paramNames, object[] values, IType[] types)
+ {
+ this.outer = template;
+ this.queryName = queryName;
+ this.paramNames = paramNames;
+ this.values = values;
+ this.types = types;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ IQuery queryObject = session.GetNamedQuery(queryName);
+ outer.PrepareQuery(queryObject);
+ if (values != null)
+ {
+ for (int i = 0; i < values.Length; i++)
+ {
+ outer.ApplyNamedParameterToQuery(queryObject, paramNames[i], values[i], (types != null ? types[i] : null));
+ }
+ }
+ return queryObject.List();
+
+ }
+ }
+
+
+ internal class FindByNamedQueryAndValueObjectHibernateCallback : IHibernateCallback
+ {
+ HibernateTemplate outer;
+ private string queryName;
+ private object valueObject;
+
+ public FindByNamedQueryAndValueObjectHibernateCallback(HibernateTemplate template, string queryName, object valueObject)
+ {
+ this.outer = template;
+ this.queryName = queryName;
+ this.valueObject = valueObject;
+
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ IQuery queryObject = session.GetNamedQuery(queryName);
+ outer.PrepareQuery(queryObject);
+ queryObject.SetProperties(valueObject);
+ return queryObject.List();
+
+ }
+
+
+
+ }
+
+ internal class FindByValueObjectHibernateCallback : IHibernateCallback
+ {
+ HibernateTemplate outer;
+ private string queryString;
+ private object valueObject;
+
+ public FindByValueObjectHibernateCallback(HibernateTemplate template, string queryString, object valueObject)
+ {
+ this.outer = template;
+ this.queryString = queryString;
+ this.valueObject = valueObject;
+
+ }
+
+ public object DoInHibernate(ISession session)
+ {
+ IQuery queryObject = session.CreateQuery(queryString);
+ outer.PrepareQuery(queryObject);
+ queryObject.SetProperties(valueObject);
+ return queryObject.List();
+
+ }
+
+ }
+
+ internal class ExecuteHibernateCallbackUsingDelegate : IHibernateCallback
+ {
+ private HibernateDelegate del;
+
+ public ExecuteHibernateCallbackUsingDelegate(HibernateDelegate d)
+ {
+ del = d;
+ }
+
+ public object DoInHibernate(ISession session)
+ {
+ return del(session);
+ }
+ }
+
+
+ internal class GetByTypeHibernateCallback : IHibernateCallback
+ {
+ private Type entityType;
+ private object id;
+ private LockMode lockMode;
+
+ public GetByTypeHibernateCallback(Type entityType, object id, LockMode lockMode)
+ {
+ this.entityType = entityType;
+ this.id = id;
+ this.lockMode = lockMode;
+
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ if (lockMode != null)
+ {
+ return session.Get(entityType, id, lockMode);
+ }
+ else
+ {
+ return session.Get(entityType, id);
+ }
+ }
+
+
+
+ }
+
+
+ internal class LoadByTypeHibernateCallback : IHibernateCallback
+ {
+ private Type entityType;
+ private object id;
+ private LockMode lockMode;
+
+ public LoadByTypeHibernateCallback(Type entityType, object id, LockMode lockMode)
+ {
+ this.entityType = entityType;
+ this.id = id;
+ this.lockMode = lockMode;
+
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ if (lockMode != null)
+ {
+ return session.Load(entityType, id, lockMode);
+ }
+ else
+ {
+ return session.Load(entityType, id);
+ }
+ }
+
+
+
+ }
+
+
+ internal class LoadByEntityHibernateCallback : IHibernateCallback
+ {
+ private object entity;
+ private object id;
+
+ public LoadByEntityHibernateCallback(object entity, object id)
+ {
+ this.entity = entity;
+ this.id = id;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ session.Load(entity, id);
+ return null;
+ }
+
+
+
+ }
+
+
+ internal class LoadAllByTypeHibernateCallback : IHibernateCallback
+ {
+ private HibernateTemplate outer;
+ private Type entityType;
+
+ public LoadAllByTypeHibernateCallback(HibernateTemplate template, Type entityType)
+ {
+ outer = template;
+ this.entityType = entityType;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ ICriteria criteria = session.CreateCriteria(entityType);
+ outer.PrepareCriteria(criteria);
+ return criteria.List();
+ }
+ }
+
+
+ internal class LockHibernateCallback : IHibernateCallback
+ {
+ private object entity;
+ private LockMode lockMode;
+
+ public LockHibernateCallback(object entity, LockMode lockMode)
+ {
+ this.entity = entity;
+ this.lockMode = lockMode;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ session.Lock(entity, lockMode);
+ return null;
+ }
+ }
+
+
+ internal class RefreshHibernateCallback : IHibernateCallback
+ {
+ private object entity;
+ private LockMode lockMode;
+
+ public RefreshHibernateCallback(object entity, LockMode lockMode)
+ {
+ this.entity = entity;
+ this.lockMode = lockMode;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ if (lockMode != null)
+ {
+ session.Refresh(entity, lockMode);
+ }
+ else
+ {
+ session.Refresh(entity);
+ }
+ return null;
+ }
+ }
+
+
+ internal class SaveObjectHibernateCallback : IHibernateCallback
+ {
+ private HibernateTemplate outer;
+ private object entity;
+
+ public SaveObjectHibernateCallback(HibernateTemplate template, object entity)
+ {
+ this.outer = template;
+ this.entity = entity;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ outer.CheckWriteOperationAllowed(session);
+ return session.Save(entity);
+ }
+ }
+
+
+ internal class SaveObjectWithIdHibernateCallback : IHibernateCallback
+ {
+ private HibernateTemplate outer;
+ private object entity;
+ private object id;
+
+ public SaveObjectWithIdHibernateCallback(HibernateTemplate template, object entity, object id)
+ {
+ this.outer = template;
+ this.entity = entity;
+ this.id = id;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ outer.CheckWriteOperationAllowed(session);
+ session.Save(entity, id);
+ return null;
+ }
+ }
+
+
+ internal class UpdateObjectHibernateCallback : IHibernateCallback
+ {
+ private HibernateTemplate outer;
+ private object entity;
+ private LockMode lockMode;
+
+ public UpdateObjectHibernateCallback(HibernateTemplate template, object entity, LockMode lockMode)
+ {
+ this.outer = template;
+ this.entity = entity;
+ this.lockMode = lockMode;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ outer.CheckWriteOperationAllowed(session);
+ session.Update(entity);
+ if (lockMode != null)
+ {
+ session.Lock(entity, lockMode);
+ }
+ return null;
+ }
+ }
+
+
+ internal class SaveOrUpdateObjectHibernateCallback : IHibernateCallback
+ {
+ private HibernateTemplate outer;
+ private object entity;
+
+ public SaveOrUpdateObjectHibernateCallback(HibernateTemplate template, object entity)
+ {
+ this.outer = template;
+ this.entity = entity;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ outer.CheckWriteOperationAllowed(session);
+ session.SaveOrUpdate(entity);
+ return null;
+ }
+ }
+
+ internal class SaveOrUpdateAllHibernateCallback : IHibernateCallback
+ {
+ private HibernateTemplate outer;
+ private ICollection entities;
+
+ public SaveOrUpdateAllHibernateCallback(HibernateTemplate template, ICollection entities)
+ {
+ this.outer = template;
+ this.entities = entities;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///
+ ///
+ public object DoInHibernate(ISession session)
+ {
+ outer.CheckWriteOperationAllowed(session);
+ foreach (object entity in entities)
+ {
+ session.SaveOrUpdate(entity);
+ }
+ return null;
+ }
+ }
+ internal class SaveOrUpdateCopyHibernateCallback : IHibernateCallback
+ {
+ private HibernateTemplate outer;
+ private object entity;
+
+ public SaveOrUpdateCopyHibernateCallback(HibernateTemplate template, object entity)
+ {
+ this.outer = template;
+ this.entity = entity;
+ }
+
+ ///
+ /// Gets called by HibernateTemplate with an active
+ /// Hibernate Session. Does not need to care about activating or closing
+ /// the Session, or handling transactions.
+ ///
+ ///
+ ///
+ /// Allows for returning a result object created within the callback, i.e.
+ /// a domain object or a collection of domain objects. Note that there's
+ /// special support for single step actions: see HibernateTemplate.find etc.
+ ///