fastcodejava
fastcodejava

Reputation: 41097

My Hibernate Interceptor is not working

I have the following configuration below :

<bean id="myHibernateInterceptor" class="com.foo.interceptor.MyHibernateInterceptor" />

<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
      </property>
      <property name="entityInterceptor" ref="myHibernateInterceptor"/>
</bean>

But my hibernate interceptor is never firing. Any clues?

Here is the interceptor code :

public class MyHibernateInterceptor extends EmptyInterceptor {

   @Override
   public boolean onFlushDirty(final Object entity, final Serializable id, 
                           final Object[] currentState, final Object[] previousState, 
                           final String[] propertyNames, final Type[] types) {
    setValue(currentState, propertyNames, "createdOn", new Date());

            return true;
    }
}

Upvotes: 0

Views: 2525

Answers (2)

Tiago Medici
Tiago Medici

Reputation: 2194

Have you put the intercepetor on the session and also you can use the sessionbuilder on the interceptor :

SessionFactory sessionFactory = getSessionFactory(); 
SessionBuilder sessionBuilder = sessionFactory.withOptions(); 
Session session = sessionBuilder.interceptor(interceptor).openSession(); interceptor.setSessionBuilder(sessionBuilder);

your interceptor must extends the class org hibernate EmptyInterceptor

the according to your busines you implement those methods onsave onupdate ondelete postflush etc

Upvotes: 0

GinoA
GinoA

Reputation: 732

I found this question while facing the same issue. My onSave() was firing fine, but my onFlushDirty() wasn't. I discovered that addng a flush() call after saveOrUpdate() was needed. Looking back, that seems obvious, but it wasn't at the time.

Hope this helps.

Upvotes: 3

Related Questions