com.interface21.orm.hibernate
Class HibernateInterceptor

java.lang.Object
  |
  +--com.interface21.orm.hibernate.HibernateInterceptor
All Implemented Interfaces:
org.aopalliance.Interceptor, org.aopalliance.MethodInterceptor

public class HibernateInterceptor
extends java.lang.Object
implements org.aopalliance.MethodInterceptor

This interceptor binds a new Hibernate Session to the thread before a method call, closing and removing it afterwards in case of any method outcome. If there already was a pre-bound Session (e.g. from HibernateTransactionManager, or from a surrounding Hibernate-intercepted method), the interceptor simply takes part in it.

Application code must retrieve a Hibernate Session via SessionFactoryUtils' getSession method, to be able to detect a thread-bound Session. It is preferable to use getSession with allowCreate=false, as the code relies on the interceptor to provide proper Session handling. Typically the code will look as follows:

public void doHibernateAction() {
  Session session = SessionFactoryUtils.getSession(this.sessionFactory, false);
  try {
    ...
  }
  catch (HibernateException ex) {
    throw SessionFactoryUtils.convertHibernateAccessException(ex);
  }
}

Note that the application must care about handling HibernateExceptions itself, preferably via delegating to SessionFactoryUtils' convertHibernateAccessException that converts them to ones that are compatible with the com.interface21.dao exception hierarchy (like HibernateTemplate does).

Unfortunately, this interceptor cannot convert checked HibernateExceptions to unchecked dao ones automatically. The intercepted method would have to throw HibernateException to be able to achieve this - thus the caller would still have to catch or rethrow it, even if it will never be thrown if intercepted.

This class can be considered a declarative alternative to HibernateTemplate's callback approach. The advantages are:

The drawbacks are:

Note: This class, like all of Spring's Hibernate support, requires Hibernate 2.0 (initially developed with RC1).

Since:
13.06.2003
Author:
Juergen Hoeller
See Also:
SessionFactoryUtils.getSession(net.sf.hibernate.SessionFactory, boolean), HibernateTransactionManager, HibernateTemplate

Constructor Summary
HibernateInterceptor()
           
 
Method Summary
 java.lang.Object invoke(org.aopalliance.MethodInvocation methodInvocation)
           
 void setForceFlush(boolean forceFlush)
          If a flush of the Hibernate Session should be forced after executing the target method.
 void setSessionFactory(net.sf.hibernate.SessionFactory sessionFactory)
          Set the SessionFactory that this instance should create thread-bound Sessions for.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateInterceptor

public HibernateInterceptor()
Method Detail

setSessionFactory

public void setSessionFactory(net.sf.hibernate.SessionFactory sessionFactory)
Set the SessionFactory that this instance should create thread-bound Sessions for.

setForceFlush

public void setForceFlush(boolean forceFlush)
If a flush of the Hibernate Session should be forced after executing the target method. By default, the interceptor will only trigger a flush if not in a Hibernate transaction, as a final flush will occur on commit anyway.

A forced flush leads to immediate synchronization with the database, even if in a Hibernate transaction. This causes inconsistencies to show up and throw a respective exception immediately. But the drawbacks are:


invoke

public java.lang.Object invoke(org.aopalliance.MethodInvocation methodInvocation)
                        throws java.lang.Throwable
Specified by:
invoke in interface org.aopalliance.MethodInterceptor


Rod Johnson and Spring contributors 2001-2003.