Reputation: 516
I have a simple interceptor
public class HistoryInterceptor extends EmptyInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(HistoryInterceptor.class);
@Override
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
LOGGER.debug("INTERCEPTOR - HERE");
return super.onLoad(entity, id, state, propertyNames, types);
}
@Override
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
LOGGER.debug("INTERCEPTOR - HERE");
}
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
LOGGER.debug("INTERCEPTOR - HERE");
return true;
}
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
LOGGER.debug("INTERCEPTOR - HERE");
return true;
}
}
that I'd like to use to update a particular field of an object just before it's persisted. This is the important part of my db-context.xml
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
</bean>
</property>
<property name="packagesToScan" value="com.common.model"/>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="com.hibernate.configs.JsonPostgreSQL9Dialect" />
<entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}" />
<entry key="hibernate.format_sql" value="true" />
<entry key="hibernate.default_schema" value="${hibernate.default_schema}"/>
</map>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Datasource Configuration -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/config/database.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
</bean>
and my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="msPU">
<class>com.common.model.OrderLine</class>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.ejb.interceptor"
value="com.hibernate.configs.HistoryInterceptor"/>
</properties>
</persistence-unit>
</persistence>
With this configurations, my interceptor is never called and I can't figure out the reason. Does anyone have a solution for this?
Upvotes: 1
Views: 1694
Reputation: 10679
Put the hibernate.ejb.interceptor
property in the jpaPropertyMap
instead of using the persistence.xml
config file. It seems that the persistence.xml
file is never used so it should fix your problem.
...
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.ejb.interceptor" value="com.hibernate.configs.HistoryInterceptor" />
<entry key="hibernate.dialect" value="com.hibernate.configs.JsonPostgreSQL9Dialect" />
<entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}" />
<entry key="hibernate.format_sql" value="true" />
<entry key="hibernate.default_schema" value="${hibernate.default_schema}"/>
</map>
</property>
...
Upvotes: 1