org.springframework.orm.hibernate
Class HibernateInterceptor

java.lang.Object
  extended byorg.springframework.orm.hibernate.HibernateAccessor
      extended byorg.springframework.orm.hibernate.HibernateInterceptor
All Implemented Interfaces:
org.aopalliance.aop.Advice, BeanFactoryAware, InitializingBean, org.aopalliance.intercept.Interceptor, org.aopalliance.intercept.MethodInterceptor

public class HibernateInterceptor
extends HibernateAccessor
implements org.aopalliance.intercept.MethodInterceptor, BeanFactoryAware

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 org.springframework.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: Spring's Hibernate support requires Hibernate 2.1 (as of Spring 1.0).

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

Field Summary
 
Fields inherited from class org.springframework.orm.hibernate.HibernateAccessor
FLUSH_AUTO, FLUSH_EAGER, FLUSH_NEVER, logger
 
Constructor Summary
HibernateInterceptor()
           
 
Method Summary
 net.sf.hibernate.Interceptor getEntityInterceptor()
          Return the current Hibernate entity interceptor, or null if none.
 java.lang.Object invoke(org.aopalliance.intercept.MethodInvocation methodInvocation)
           
 void setBeanFactory(BeanFactory beanFactory)
          The bean factory just needs to be known for resolving entity interceptor bean names.
 void setEntityInterceptorBeanName(java.lang.String entityInterceptorBeanName)
          Set the bean name of a Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database.
 
Methods inherited from class org.springframework.orm.hibernate.HibernateAccessor
afterPropertiesSet, convertHibernateAccessException, convertJdbcAccessException, flushIfNecessary, getFlushMode, getJdbcExceptionTranslator, getSessionFactory, setEntityInterceptor, setFlushMode, setFlushModeName, setJdbcExceptionTranslator, setSessionFactory
 
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

setEntityInterceptorBeanName

public void setEntityInterceptorBeanName(java.lang.String entityInterceptorBeanName)
Set the bean 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 bean factory to be known, to be able to resolve the bean 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.

Parameters:
entityInterceptorBeanName - the name of the entity interceptor in the bean factory
See Also:
setBeanFactory(org.springframework.beans.factory.BeanFactory), HibernateAccessor.setEntityInterceptor(net.sf.hibernate.Interceptor)

getEntityInterceptor

public net.sf.hibernate.Interceptor getEntityInterceptor()
                                                  throws java.lang.IllegalStateException,
                                                         BeansException
Return the current Hibernate entity interceptor, or null if none. Resolves an entity interceptor bean name via the bean factory, if necessary.

Overrides:
getEntityInterceptor in class HibernateAccessor
Throws:
java.lang.IllegalStateException - if bean name specified but no bean factory set
BeansException - if bean name resolution via the bean factory failed
See Also:
HibernateAccessor.setEntityInterceptor(net.sf.hibernate.Interceptor), setEntityInterceptorBeanName(java.lang.String), setBeanFactory(org.springframework.beans.factory.BeanFactory)

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
The bean factory just needs to be known for resolving entity interceptor bean names. It does not need to be set for any other mode of operation.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (may not be null). The bean can immediately call methods on the factory.
See Also:
setEntityInterceptorBeanName(java.lang.String)

invoke

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


Copyright (C) 2003-2004 The Spring Framework Project.