Spring 应用程序未调用 Hibernate 拦截器
Hibernate Interceptors doesn't get called in Spring application
我有一个简单的拦截器
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;
}
}
我想用它来更新对象的特定字段,就在它被持久化之前。这是我的 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>
和我的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>
使用此配置,我的拦截器从未被调用,我无法找出原因。
有人对此有解决方案吗?
将 hibernate.ejb.interceptor
属性 放入 jpaPropertyMap
而不是使用 persistence.xml
配置文件。似乎从未使用过 persistence.xml
文件,因此它应该可以解决您的问题。
...
<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>
...
我有一个简单的拦截器
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;
}
}
我想用它来更新对象的特定字段,就在它被持久化之前。这是我的 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>
和我的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>
使用此配置,我的拦截器从未被调用,我无法找出原因。 有人对此有解决方案吗?
将 hibernate.ejb.interceptor
属性 放入 jpaPropertyMap
而不是使用 persistence.xml
配置文件。似乎从未使用过 persistence.xml
文件,因此它应该可以解决您的问题。
...
<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>
...