类 SessionFactoryUtils
- java.lang.Object
- org.springframework.orm.hibernate3.SessionFactoryUtils
@Deprecated public abstract class SessionFactoryUtils extends Object
已过时。as of Spring 4.3, in favor of Hibernate 4.x/5.xHelper class featuring methods for Hibernate Session handling, allowing for reuse of Hibernate Session instances within transactions. Also provides support for exception translation.Supports synchronization with both Spring-managed JTA transactions (see
JtaTransactionManager
) and non-Spring JTA transactions (i.e. plain JTA or EJB CMT), transparently providing transaction-scoped Hibernate Sessions. Note that for non-Spring JTA transactions, a JTA TransactionManagerLookup has to be specified in the Hibernate configuration.Used internally by
HibernateTemplate
,HibernateInterceptor
andHibernateTransactionManager
. Can also be used directly in application code.Requires Hibernate 3.6.x, as of Spring 4.0.
字段概要
字段 修饰符和类型 字段 说明 static int
SESSION_SYNCHRONIZATION_ORDER
已过时。Order value for TransactionSynchronization objects that clean up Hibernate Sessions.
构造器概要
构造器 构造器 说明 SessionFactoryUtils()
已过时。
方法概要
所有方法 静态方法 具体方法 已过时的方法 修饰符和类型 方法 说明 static void
applyTransactionTimeout(Criteria criteria, SessionFactory sessionFactory)
已过时。Apply the current transaction timeout, if any, to the given Hibernate Criteria object.static void
applyTransactionTimeout(Query query, SessionFactory sessionFactory)
已过时。Apply the current transaction timeout, if any, to the given Hibernate Query object.static void
closeSession(Session session)
已过时。Perform actual closing of the Hibernate Session, catching and logging any cleanup exceptions thrown.static DataAccessException
convertHibernateAccessException(HibernateException ex)
已过时。Convert the given HibernateException to an appropriate exception from theorg.springframework.dao
hierarchy.static Session
doGetSession(SessionFactory sessionFactory, boolean allowCreate)
已过时。Get a Hibernate Session for the given SessionFactory.static DataSource
getDataSource(SessionFactory sessionFactory)
已过时。Determine the DataSource of the given SessionFactory.static TransactionManager
getJtaTransactionManager(SessionFactory sessionFactory, Session session)
已过时。Try to retrieve the JTA TransactionManager from the given SessionFactory and/or Session.static Session
getNewSession(SessionFactory sessionFactory)
已过时。Get a new Hibernate Session from the given SessionFactory.static Session
getNewSession(SessionFactory sessionFactory, Interceptor entityInterceptor)
已过时。Get a new Hibernate Session from the given SessionFactory.static Session
getSession(SessionFactory sessionFactory, boolean allowCreate)
已过时。Get a Hibernate Session for the given SessionFactory.static Session
getSession(SessionFactory sessionFactory, Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator)
已过时。Get a Hibernate Session for the given SessionFactory.static boolean
hasTransactionalSession(SessionFactory sessionFactory)
已过时。Return whether there is a transactional Hibernate Session for the current thread, that is, a Session bound to the current thread by Spring's transaction facilities.static void
initDeferredClose(SessionFactory sessionFactory)
已过时。Initialize deferred close for the current thread and the given SessionFactory.static boolean
isDeferredCloseActive(SessionFactory sessionFactory)
已过时。Determine whether deferred close is active for the current thread and the given SessionFactory.static boolean
isSessionTransactional(Session session, SessionFactory sessionFactory)
已过时。Return whether the given Hibernate Session is transactional, that is, bound to the current thread by Spring's transaction facilities.static SQLExceptionTranslator
newJdbcExceptionTranslator(SessionFactory sessionFactory)
已过时。Create an appropriate SQLExceptionTranslator for the given SessionFactory.static void
processDeferredClose(SessionFactory sessionFactory)
已过时。Process all Hibernate Sessions that have been registered for deferred close for the given SessionFactory.static void
releaseSession(Session session, SessionFactory sessionFactory)
已过时。Close the given Session, created via the given factory, if it is not managed externally (i.e. not bound to the thread).static String
toString(Session session)
已过时。Stringify the given Session for debug logging.
字段详细资料
SESSION_SYNCHRONIZATION_ORDER
public static final int SESSION_SYNCHRONIZATION_ORDER
已过时。Order value for TransactionSynchronization objects that clean up Hibernate Sessions. ReturnsDataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100
to execute Session cleanup before JDBC Connection cleanup, if any.
构造器详细资料
SessionFactoryUtils
public SessionFactoryUtils()
已过时。
方法详细资料
getDataSource
public static DataSource getDataSource(SessionFactory sessionFactory)
已过时。Determine the DataSource of the given SessionFactory.- 参数:
sessionFactory
- the SessionFactory to check- 返回:
- the DataSource, or
null
if none found - 另请参阅:
SessionFactoryImplementor.getConnectionProvider()
,LocalDataSourceConnectionProvider
newJdbcExceptionTranslator
public static SQLExceptionTranslator newJdbcExceptionTranslator(SessionFactory sessionFactory)
已过时。Create an appropriate SQLExceptionTranslator for the given SessionFactory. If a DataSource is found, a SQLErrorCodeSQLExceptionTranslator for the DataSource is created; else, a SQLStateSQLExceptionTranslator as fallback.- 参数:
sessionFactory
- the SessionFactory to create the translator for- 返回:
- the SQLExceptionTranslator
- 另请参阅:
getDataSource(org.hibernate.SessionFactory)
,SQLErrorCodeSQLExceptionTranslator
,SQLStateSQLExceptionTranslator
getJtaTransactionManager
public static TransactionManager getJtaTransactionManager(SessionFactory sessionFactory, Session session)
已过时。Try to retrieve the JTA TransactionManager from the given SessionFactory and/or Session. Check the passed-in SessionFactory for implementing SessionFactoryImplementor (the usual case), falling back to the SessionFactory reference that the Session itself carries.- 参数:
sessionFactory
- Hibernate SessionFactorysession
- Hibernate Session (can also benull
)- 返回:
- the JTA TransactionManager, if any
- 另请参阅:
TransactionManager
,SessionFactoryImplementor.getTransactionManager()
,Session.getSessionFactory()
,SessionFactoryImpl
getSession
public static Session getSession(SessionFactory sessionFactory, boolean allowCreate) throws DataAccessResourceFailureException, IllegalStateException
已过时。Get a Hibernate Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when usingHibernateTransactionManager
. Will create a new Session otherwise, if "allowCreate" istrue
.This is the
getSession
method used by typical data access code, in combination withreleaseSession
called when done with the Session. Note that HibernateTemplate allows to write data access code without caring about such resource handling.- 参数:
sessionFactory
- Hibernate SessionFactory to create the session withallowCreate
- whether a non-transactional Session should be created when no transactional Session can be found for the current thread- 返回:
- the Hibernate Session
- 抛出:
DataAccessResourceFailureException
- if the Session couldn't be createdIllegalStateException
- if no thread-bound Session found and "allowCreate" isfalse
- 另请参阅:
getSession(SessionFactory, Interceptor, SQLExceptionTranslator)
,releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
,HibernateTemplate
getSession
public static Session getSession(SessionFactory sessionFactory, Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException
已过时。Get a Hibernate Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when usingHibernateTransactionManager
. Will always create a new Session otherwise.Supports setting a Session-level Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database. Such an interceptor can also be set at the SessionFactory level (i.e. on LocalSessionFactoryBean), on HibernateTransactionManager, etc.
- 参数:
sessionFactory
- Hibernate SessionFactory to create the session withentityInterceptor
- Hibernate entity interceptor, ornull
if nonejdbcExceptionTranslator
- SQLExcepionTranslator to use for flushing the Session on transaction synchronization (may benull
; only used when actually registering a transaction synchronization)- 返回:
- the Hibernate Session
- 抛出:
DataAccessResourceFailureException
- if the Session couldn't be created- 另请参阅:
LocalSessionFactoryBean.setEntityInterceptor(org.hibernate.Interceptor)
,HibernateAccessor.setEntityInterceptor(org.hibernate.Interceptor)
doGetSession
public static Session doGetSession(SessionFactory sessionFactory, boolean allowCreate) throws HibernateException, IllegalStateException
已过时。Get a Hibernate Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when usingHibernateTransactionManager
. Will create a new Session otherwise, if "allowCreate" istrue
.Throws the original HibernateException, in contrast to
getSession(org.hibernate.SessionFactory, boolean)
.- 参数:
sessionFactory
- Hibernate SessionFactory to create the session withallowCreate
- whether a non-transactional Session should be created when no transactional Session can be found for the current thread- 返回:
- the Hibernate Session
- 抛出:
HibernateException
- if the Session couldn't be createdIllegalStateException
- if no thread-bound Session found and allowCreate false
getNewSession
public static Session getNewSession(SessionFactory sessionFactory)
已过时。Get a new Hibernate Session from the given SessionFactory. Will return a new Session even if there already is a pre-bound Session for the given SessionFactory.Within a transaction, this method will create a new Session that shares the transaction's JDBC Connection. More specifically, it will use the same JDBC Connection as the pre-bound Hibernate Session.
- 参数:
sessionFactory
- Hibernate SessionFactory to create the session with- 返回:
- the new Session
getNewSession
public static Session getNewSession(SessionFactory sessionFactory, Interceptor entityInterceptor)
已过时。Get a new Hibernate Session from the given SessionFactory. Will return a new Session even if there already is a pre-bound Session for the given SessionFactory.Within a transaction, this method will create a new Session that shares the transaction's JDBC Connection. More specifically, it will use the same JDBC Connection as the pre-bound Hibernate Session.
- 参数:
sessionFactory
- Hibernate SessionFactory to create the session withentityInterceptor
- Hibernate entity interceptor, ornull
if none- 返回:
- the new Session
toString
public static String toString(Session session)
已过时。Stringify the given Session for debug logging. Returns output equivalent toObject.toString()
: the fully qualified class name + "@" + the identity hash code.The sole reason why this is necessary is because Hibernate3's
Session.toString()
implementation is broken (and won't be fixed): it logs the toString representation of all persistent objects in the Session, which might lead to ConcurrentModificationExceptions if the persistent objects in turn refer to the Session (for example, for lazy loading).- 参数:
session
- the Hibernate Session to stringify- 返回:
- the String representation of the given Session
hasTransactionalSession
public static boolean hasTransactionalSession(SessionFactory sessionFactory)
已过时。Return whether there is a transactional Hibernate Session for the current thread, that is, a Session bound to the current thread by Spring's transaction facilities.- 参数:
sessionFactory
- Hibernate SessionFactory to check (may benull
)- 返回:
- whether there is a transactional Session for current thread
isSessionTransactional
public static boolean isSessionTransactional(Session session, SessionFactory sessionFactory)
已过时。Return whether the given Hibernate Session is transactional, that is, bound to the current thread by Spring's transaction facilities.- 参数:
session
- the Hibernate Session to checksessionFactory
- Hibernate SessionFactory that the Session was created with (may benull
)- 返回:
- whether the Session is transactional
applyTransactionTimeout
public static void applyTransactionTimeout(Query query, SessionFactory sessionFactory)
已过时。Apply the current transaction timeout, if any, to the given Hibernate Query object.- 参数:
query
- the Hibernate Query objectsessionFactory
- Hibernate SessionFactory that the Query was created for (may benull
)- 另请参阅:
Query.setTimeout(int)
applyTransactionTimeout
public static void applyTransactionTimeout(Criteria criteria, SessionFactory sessionFactory)
已过时。Apply the current transaction timeout, if any, to the given Hibernate Criteria object.- 参数:
criteria
- the Hibernate Criteria objectsessionFactory
- Hibernate SessionFactory that the Criteria was created for- 另请参阅:
Criteria.setTimeout(int)
convertHibernateAccessException
public static DataAccessException convertHibernateAccessException(HibernateException ex)
已过时。Convert the given HibernateException to an appropriate exception from theorg.springframework.dao
hierarchy.- 参数:
ex
- HibernateException that occurred- 返回:
- the corresponding DataAccessException instance
- 另请参阅:
HibernateAccessor.convertHibernateAccessException(org.hibernate.HibernateException)
,HibernateTransactionManager.convertHibernateAccessException(org.hibernate.HibernateException)
isDeferredCloseActive
public static boolean isDeferredCloseActive(SessionFactory sessionFactory)
已过时。Determine whether deferred close is active for the current thread and the given SessionFactory.- 参数:
sessionFactory
- the Hibernate SessionFactory to check- 返回:
- whether deferred close is active
initDeferredClose
public static void initDeferredClose(SessionFactory sessionFactory)
已过时。Initialize deferred close for the current thread and the given SessionFactory. Sessions will not be actually closed on close calls then, but rather at aprocessDeferredClose(org.hibernate.SessionFactory)
call at a finishing point (like request completion).Used by
OpenSessionInViewFilter
andOpenSessionInViewInterceptor
when not configured for a single session.- 参数:
sessionFactory
- the Hibernate SessionFactory to initialize deferred close for- 另请参阅:
processDeferredClose(org.hibernate.SessionFactory)
,releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
,OpenSessionInViewFilter.setSingleSession(boolean)
,OpenSessionInViewInterceptor.setSingleSession(boolean)
processDeferredClose
public static void processDeferredClose(SessionFactory sessionFactory)
已过时。Process all Hibernate Sessions that have been registered for deferred close for the given SessionFactory.- 参数:
sessionFactory
- the Hibernate SessionFactory to process deferred close for- 另请参阅:
initDeferredClose(org.hibernate.SessionFactory)
,releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
releaseSession
public static void releaseSession(Session session, SessionFactory sessionFactory)
已过时。Close the given Session, created via the given factory, if it is not managed externally (i.e. not bound to the thread).- 参数:
session
- the Hibernate Session to close (may benull
)sessionFactory
- Hibernate SessionFactory that the Session was created with (may benull
)
closeSession
public static void closeSession(Session session)
已过时。Perform actual closing of the Hibernate Session, catching and logging any cleanup exceptions thrown.- 参数:
session
- the Hibernate Session to close (may benull
)- 另请参阅:
Session.close()