org.springframework.orm.hibernate
Class HibernateAccessor

Direct Known Subclasses:
HibernateInterceptor, HibernateTemplate, OpenSessionInViewInterceptor

public abstract class HibernateAccessor
implements InitializingBean

Base class for HibernateTemplate and HibernateInterceptor, defining common properties like flushing behavior.

Not intended to be used directly. See HibernateTemplate and HibernateInterceptor.

Author:
Juergen Hoeller
Since: 29.07.2003
See Also: HibernateTemplate, HibernateInterceptor, setFlushMode(int)

Field Summary
 final static intFLUSH_AUTO
          Automatic flushing is the default mode for a Hibernate session.
 final static intFLUSH_EAGER
          Eager flushing leads to immediate synchronization with the database, even if in a transaction.
 final static intFLUSH_NEVER
          Never flush is a good strategy for read-only units of work.

Method Summary
 voidafterPropertiesSet()
           
 DataAccessExceptionconvertHibernateAccessException(HibernateException ex)
          Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.
 voidflushIfNecessary(Session session, boolean existingTransaction)
          Flush the given Hibernate session if necessary.
 InterceptorgetEntityInterceptor()
          Return the current Hibernate entity interceptor, or null if none.
 intgetFlushMode()
          Return if a flush should be forced after executing the callback code.
 SQLExceptionTranslatorgetJdbcExceptionTranslator()
          Return the JDBC exception translator for this instance.
 SessionFactorygetSessionFactory()
          Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
 voidsetEntityInterceptor(Interceptor entityInterceptor)
          Set a Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database.
 voidsetFlushMode(int flushMode)
          Set the flush behavior to one of the constants in this class.
 voidsetFlushModeName(String constantName)
          Set the flush behavior by the name of the respective constant in this class, e.g.
 voidsetJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)
          Set the JDBC exception translator for this instance.
 voidsetSessionFactory(SessionFactory sessionFactory)
          Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.

Field Detail

FLUSH_AUTO

public final static int FLUSH_AUTO
Automatic flushing is the default mode for a Hibernate session. A session will get flushed on transaction commit or session closing, and on certain find operations that might involve already modified instances, but not after each unit of work like with eager flushing.
See Also:
setFlushMode(int)

FLUSH_EAGER

public final static int FLUSH_EAGER
Eager flushing leads to immediate synchronization with the database, even if in a transaction. This causes inconsistencies to show up and throw a respective exception immediately, and JDBC access code that participates in the same transaction will see the changes as the database is already aware of them then. But the drawbacks are:
See Also:
setFlushMode(int)

FLUSH_NEVER

public final static int FLUSH_NEVER
Never flush is a good strategy for read-only units of work. Hibernate will not track and look for changes in this case, avoiding any overhead of modification detection.
See Also:
setFlushMode(int)
Method Detail

afterPropertiesSet

public void afterPropertiesSet()

convertHibernateAccessException

public DataAccessException convertHibernateAccessException(HibernateException ex)
Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy. Will automatically detect wrapped SQLExceptions and convert them accordingly.

The default implementation delegates to SessionFactoryUtils and convertJdbcAccessException. Can be overridden in subclasses.

Parameters:
ex - HibernateException that occured
Returns: the corresponding DataAccessException instance
See Also:
convertJdbcAccessException(java.sql.SQLException), SessionFactoryUtils.convertHibernateAccessException(HibernateException)

flushIfNecessary

public void flushIfNecessary(Session session, boolean existingTransaction)
throws HibernateException
Flush the given Hibernate session if necessary.
Parameters:
session - the current Hibernate session
existingTransaction - if executing within an existing transaction
Throws:
HibernateException - in case of Hibernate flushing errors

getEntityInterceptor

public Interceptor getEntityInterceptor()
Return the current Hibernate entity interceptor, or null if none.

getFlushMode

public int getFlushMode()
Return if a flush should be forced after executing the callback code.

getJdbcExceptionTranslator

public SQLExceptionTranslator getJdbcExceptionTranslator()
Return the JDBC exception translator for this instance.

getSessionFactory

public SessionFactory getSessionFactory()
Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.

setEntityInterceptor

public void setEntityInterceptor(Interceptor entityInterceptor)
Set 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 object.

Such an interceptor can either be set at the SessionFactory level, i.e. on LocalSessionFactoryBean, or at the Session level, i.e. on HibernateTemplate, HibernateInterceptor, and HibernateTransactionManager. It's preferable to set it on LocalSessionFactoryBean or HibernateTransactionManager to avoid repeated configuration and guarantee consistent behavior in transactions.

See Also:
LocalSessionFactoryBean.setEntityInterceptor(Interceptor), HibernateTransactionManager.setEntityInterceptor(Interceptor)

setFlushMode

public void setFlushMode(int flushMode)
Set the flush behavior to one of the constants in this class. Default is FLUSH_AUTO. Will get applied to any new Session created by this object.
See Also:
setFlushModeName(java.lang.String), FLUSH_AUTO

setFlushModeName

public void setFlushModeName(String constantName)
Set the flush behavior by the name of the respective constant in this class, e.g. "FLUSH_AUTO". Default is FLUSH_AUTO. Will get applied to any new Session created by this object.
Parameters:
constantName - name of the constant
See Also:
setFlushMode(int), FLUSH_AUTO

setJdbcExceptionTranslator

public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)
Set the JDBC exception translator for this instance. Applied to SQLExceptions thrown by callback code, be it direct SQLExceptions or wrapped HibernateJDBCExceptions.

The default exception translator evaluates the exception's SQLState.

Parameters:
jdbcExceptionTranslator - exception translator
See Also:
SQLStateSQLExceptionTranslator, SQLErrorCodeSQLExceptionTranslator

setSessionFactory

public void setSessionFactory(SessionFactory sessionFactory)
Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.

Association Links

to Class org.springframework.core.Constants

Constants instance for HibernateAccessor

to Class org.springframework.jdbc.support.SQLExceptionTranslator