views:

51

answers:

1

I record a history of all changes to some entities and am about to implement a mechanism similar to Envers to take care of this automatically. My question is whether to use Hibernate interceptors or their event system?

It seems like interceptors is a little simpler and does all I need. And Hibernate's own documentation suggests using interceptors for "tracking audit information." But their audit information isn't in a companion table for each entity and Envers uses the event system I imagine for a reason.

I'm using Spring 3.0 and Hibernate 3.5 (latest stables).

UPDATE: database triggers are not desirable for this situation. I'm eager to hear thoughts on hibernate interceptors vs events for audit trails/change histories.

A: 

I think the essential difference between the two is that you can only define one Interceptor in your SessionFactory, but you can register many listeners for a particular event.

I'm going to use Interceptors since I only want something very simple: a timestamp when my entity is created (the @Version annotation takes care of updating the lastModified property).

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
    <property name="packagesToScan" value="com.x.domain" />
    <property name="entityInterceptor" ref="onSaveInterceptor" />
</bean>

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/events.html#objectstate-interceptors

Eliseo Soto