类 AbstractSessionFactoryBean
- java.lang.Object
- org.springframework.orm.hibernate3.HibernateExceptionTranslator
- org.springframework.orm.hibernate3.AbstractSessionFactoryBean
- 所有已实现的接口:
DisposableBean
,FactoryBean<SessionFactory>
,InitializingBean
,PersistenceExceptionTranslator
- 直接已知子类:
LocalSessionFactoryBean
@Deprecated public abstract class AbstractSessionFactoryBean extends HibernateExceptionTranslator implements FactoryBean<SessionFactory>, InitializingBean, DisposableBean
已过时。as of Spring 4.3, in favor of Hibernate 4.x/5.xAbstractFactoryBean
that creates a HibernateSessionFactory
within a Spring application context, providing general infrastructure not related to Hibernate's specific configuration API.This class implements the
PersistenceExceptionTranslator
interface, as autodetected by Spring'sPersistenceExceptionTranslationPostProcessor
, for AOP-based translation of native exceptions to Spring DataAccessExceptions. Hence, the presence of e.g. LocalSessionFactoryBean automatically enables a PersistenceExceptionTranslationPostProcessor to translate Hibernate exceptions.This class mainly serves as common base class for
LocalSessionFactoryBean
. For details on typical SessionFactory setup, see the LocalSessionFactoryBean javadoc.- 从以下版本开始:
- 2.0
- 作者:
- Juergen Hoeller
- 另请参阅:
setExposeTransactionAwareSessionFactory(boolean)
,SessionFactory.getCurrentSession()
,PersistenceExceptionTranslationPostProcessor
构造器概要
构造器 构造器 说明 AbstractSessionFactoryBean()
已过时。
方法概要
所有方法 实例方法 抽象方法 具体方法 已过时的方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
已过时。Build and expose the SessionFactory.protected void
afterSessionFactoryCreation()
已过时。Hook that allows post-processing after the SessionFactory has been successfully created.protected void
beforeSessionFactoryDestruction()
已过时。Hook that allows shutdown processing before the SessionFactory will be closed.protected abstract SessionFactory
buildSessionFactory()
已过时。Build the underlying Hibernate SessionFactory.void
destroy()
已过时。Close the SessionFactory on bean factory shutdown.DataSource
getDataSource()
已过时。Return the DataSource to be used by the SessionFactory.SessionFactory
getObject()
已过时。Return the singleton SessionFactory.Class<? extends SessionFactory>
getObjectType()
已过时。Return the type of object that this FactoryBean creates, ornull
if not known in advance.protected SessionFactory
getSessionFactory()
已过时。Return the exposed SessionFactory.protected boolean
isExposeTransactionAwareSessionFactory()
已过时。Return whether to expose a transaction-aware proxy for the SessionFactory.boolean
isSingleton()
已过时。Is the object managed by this factory a singleton?protected boolean
isUseTransactionAwareDataSource()
已过时。Return whether to use a transaction-aware DataSource for the SessionFactory.void
setDataSource(DataSource dataSource)
已过时。Set the DataSource to be used by the SessionFactory.void
setExposeTransactionAwareSessionFactory(boolean exposeTransactionAwareSessionFactory)
已过时。Set whether to expose a transaction-aware current Session from the SessionFactory'sgetCurrentSession()
method, returning the Session that's associated with the current Spring-managed transaction, if any.void
setUseTransactionAwareDataSource(boolean useTransactionAwareDataSource)
已过时。Set whether to use a transaction-aware DataSource for the SessionFactory, i.e. whether to automatically wrap the passed-in DataSource with Spring's TransactionAwareDataSourceProxy.protected SessionFactory
wrapSessionFactoryIfNecessary(SessionFactory rawSf)
已过时。Wrap the given SessionFactory with a proxy, if demanded.从类继承的方法 org.springframework.orm.hibernate3.HibernateExceptionTranslator
convertHibernateAccessException, setJdbcExceptionTranslator, translateExceptionIfPossible
构造器详细资料
AbstractSessionFactoryBean
public AbstractSessionFactoryBean()
已过时。
方法详细资料
setDataSource
public void setDataSource(DataSource dataSource)
已过时。Set the DataSource to be used by the SessionFactory. If set, this will override corresponding settings in Hibernate properties.If this is set, the Hibernate settings should not define a connection provider to avoid meaningless double configuration.
If using HibernateTransactionManager as transaction strategy, consider proxying your target DataSource with a LazyConnectionDataSourceProxy. This defers fetching of an actual JDBC Connection until the first JDBC Statement gets executed, even within JDBC transactions (as performed by HibernateTransactionManager). Such lazy fetching is particularly beneficial for read-only operations, in particular if the chances of resolving the result in the second-level cache are high.
As JTA and transactional JNDI DataSources already provide lazy enlistment of JDBC Connections, LazyConnectionDataSourceProxy does not add value with JTA (i.e. Spring's JtaTransactionManager) as transaction strategy.
getDataSource
public DataSource getDataSource()
已过时。Return the DataSource to be used by the SessionFactory.
setUseTransactionAwareDataSource
public void setUseTransactionAwareDataSource(boolean useTransactionAwareDataSource)
已过时。Set whether to use a transaction-aware DataSource for the SessionFactory, i.e. whether to automatically wrap the passed-in DataSource with Spring's TransactionAwareDataSourceProxy.Default is "false": LocalSessionFactoryBean is usually used with Spring's HibernateTransactionManager or JtaTransactionManager, both of which work nicely on a plain JDBC DataSource. Hibernate Sessions and their JDBC Connections are fully managed by the Hibernate/JTA transaction infrastructure in such a scenario.
If you switch this flag to "true", Spring's Hibernate access will be able to participate in JDBC-based transactions managed outside of Hibernate (for example, by Spring's DataSourceTransactionManager). This can be convenient if you need a different local transaction strategy for another O/R mapping tool, for example, but still want Hibernate access to join into those transactions.
A further benefit of this option is that plain Sessions opened directly via the SessionFactory, outside of Spring's Hibernate support, will still participate in active Spring-managed transactions. However, consider using Hibernate's
getCurrentSession()
method instead (see javadoc of "exposeTransactionAwareSessionFactory" property).WARNING: When using a transaction-aware JDBC DataSource in combination with OpenSessionInViewFilter/Interceptor, whether participating in JTA or external JDBC-based transactions, it is strongly recommended to set Hibernate's Connection release mode to "after_transaction" or "after_statement", which guarantees proper Connection handling in such a scenario. In contrast to that, HibernateTransactionManager generally requires release mode "on_close".
Note: If you want to use Hibernate's Connection release mode "after_statement" with a DataSource specified on this LocalSessionFactoryBean (for example, a JTA-aware DataSource fetched from JNDI), switch this setting to "true". Otherwise, the ConnectionProvider used underneath will vote against aggressive release and thus silently switch to release mode "after_transaction".
isUseTransactionAwareDataSource
protected boolean isUseTransactionAwareDataSource()
已过时。Return whether to use a transaction-aware DataSource for the SessionFactory.
setExposeTransactionAwareSessionFactory
public void setExposeTransactionAwareSessionFactory(boolean exposeTransactionAwareSessionFactory)
已过时。Set whether to expose a transaction-aware current Session from the SessionFactory'sgetCurrentSession()
method, returning the Session that's associated with the current Spring-managed transaction, if any.Default is "true", letting data access code work with the plain Hibernate SessionFactory and its
getCurrentSession()
method, while still being able to participate in current Spring-managed transactions: with any transaction management strategy, either local or JTA / EJB CMT, and any transaction synchronization mechanism, either Spring or JTA. Furthermore,getCurrentSession()
will also seamlessly work with a request-scoped Session managed by OpenSessionInViewFilter/Interceptor.Turn this flag off to expose the plain Hibernate SessionFactory with Hibernate's default
getCurrentSession()
behavior, supporting plain JTA synchronization only. Alternatively, simply override the corresponding Hibernate property "hibernate.current_session_context_class".
isExposeTransactionAwareSessionFactory
protected boolean isExposeTransactionAwareSessionFactory()
已过时。Return whether to expose a transaction-aware proxy for the SessionFactory.
afterPropertiesSet
public void afterPropertiesSet() throws Exception
已过时。Build and expose the SessionFactory.- 指定者:
afterPropertiesSet
在接口中InitializingBean
- 抛出:
Exception
- in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason- 另请参阅:
buildSessionFactory()
,wrapSessionFactoryIfNecessary(org.hibernate.SessionFactory)
wrapSessionFactoryIfNecessary
protected SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf)
已过时。Wrap the given SessionFactory with a proxy, if demanded.The default implementation simply returns the given SessionFactory as-is. Subclasses may override this to implement transaction awareness through a SessionFactory proxy, for example.
- 参数:
rawSf
- the raw SessionFactory as built bybuildSessionFactory()
- 返回:
- the SessionFactory reference to expose
- 另请参阅:
buildSessionFactory()
getSessionFactory
protected final SessionFactory getSessionFactory()
已过时。Return the exposed SessionFactory. Will throw an exception if not initialized yet.- 返回:
- the SessionFactory (never
null
) - 抛出:
IllegalStateException
- if the SessionFactory has not been initialized yet
destroy
public void destroy() throws HibernateException
已过时。Close the SessionFactory on bean factory shutdown.- 指定者:
destroy
在接口中DisposableBean
- 抛出:
HibernateException
getObject
public SessionFactory getObject()
已过时。Return the singleton SessionFactory.- 指定者:
getObject
在接口中FactoryBean<SessionFactory>
- 返回:
- an instance of the bean (can be
null
) - 另请参阅:
FactoryBeanNotInitializedException
getObjectType
public Class<? extends SessionFactory> getObjectType()
已过时。从接口复制的说明:FactoryBean
Return the type of object that this FactoryBean creates, ornull
if not known in advance.This allows one to check for specific types of beans without instantiating objects, for example on autowiring.
In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.
This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.
NOTE: Autowiring will simply ignore FactoryBeans that return
null
here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.- 指定者:
getObjectType
在接口中FactoryBean<SessionFactory>
- 返回:
- the type of object that this FactoryBean creates, or
null
if not known at the time of the call - 另请参阅:
ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
isSingleton
public boolean isSingleton()
已过时。从接口复制的说明:FactoryBean
Is the object managed by this factory a singleton? That is, willFactoryBean.getObject()
always return the same object (a reference that can be cached)?NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from
getObject()
might get cached by the owning BeanFactory. Hence, do not returntrue
unless the FactoryBean always exposes the same reference.The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.
NOTE: This method returning
false
does not necessarily indicate that returned objects are independent instances. An implementation of the extendedSmartFactoryBean
interface may explicitly indicate independent instances through itsSmartFactoryBean.isPrototype()
method. PlainFactoryBean
implementations which do not implement this extended interface are simply assumed to always return independent instances if theisSingleton()
implementation returnsfalse
.- 指定者:
isSingleton
在接口中FactoryBean<SessionFactory>
- 返回:
- whether the exposed object is a singleton
- 另请参阅:
FactoryBean.getObject()
,SmartFactoryBean.isPrototype()
buildSessionFactory
protected abstract SessionFactory buildSessionFactory() throws Exception
已过时。Build the underlying Hibernate SessionFactory.- 返回:
- the raw SessionFactory (potentially to be wrapped with a transaction-aware proxy before it is exposed to the application)
- 抛出:
Exception
- in case of initialization failure
afterSessionFactoryCreation
protected void afterSessionFactoryCreation() throws Exception
已过时。Hook that allows post-processing after the SessionFactory has been successfully created. The SessionFactory is already available throughgetSessionFactory()
at this point.This implementation is empty.
- 抛出:
Exception
- in case of initialization failure- 另请参阅:
getSessionFactory()
beforeSessionFactoryDestruction
protected void beforeSessionFactoryDestruction()
已过时。Hook that allows shutdown processing before the SessionFactory will be closed. The SessionFactory is still available throughgetSessionFactory()
at this point.This implementation is empty.
- 另请参阅:
getSessionFactory()