我正在使用Spring,Hibernate和Struts开发一个Web应用程序。对于我的域实体类,我有2个java.util.Date变量,我想使用Hibernate Interceptor设置。
每个实体类都有以下2个我想设置的变量。
private Date createDate; private Date updateDate;
我编写了代码并完成了所需的配置,不知怎的,实体永远不会被截获。拦截器使用Spring注入SessionFactory。我在AuditInterceptor类上添加了断点。他们从未被援引过。代码和配置如下。非常感谢任何帮助。
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue
private Long id;
@Column(name = "create_date", nullable = false)
private Date createDate;
@Column(name = "update_date", nullable = false)
private Date updateDate;
@Column(name = "version_number")
@Version
private int versionNumber;
// getters and setters
}
public class AuditInterceptor extends EmptyInterceptor {
public boolean onFlushDirty(Object entity, Serializable id,
Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) throws CallbackException{
setValue(currentState, propertyNames, "updateDate", new Date());
return true;
}
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) throws CallbackException{
setValue(state, propertyNames, "createDate", new Date());
setValue(state, propertyNames, "updateDate", new Date());
return true;
}
private void setValue(Object[] state, String[] propertyNames,
String propertyToSet, Object value) {
int index = Arrays.asList(propertyNames).indexOf(propertyToSet);
if (index >= 0) {
state[index] = value;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:properties/*.properties</value>
</list>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="entityInterceptor" ref="AuditInterceptor"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.release_mode">after_statement</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.default_batch_fetch_size">10</prop>
</props>
</property>
<property name="namingStrategy" ref="hibernateNamingStrategy" />
<property name="annotatedClasses">
<list>
<!-- Define entities (domain objects here) -->
<value>com.myproj.domain.Person</value>
</list>
</property>
<property name="useTransactionAwareDataSource" value="true" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="AuditInterceptor" class="com.myproj.interceptor.AuditInterceptor"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource">
<jee:jndi-lookup jndi-name="${datasource.jndi.name}" />
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean name="hibernateNamingStrategy" class="org.hibernate.cfg.ImprovedNamingStrategy" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>