Hibernate实体拦截器触发器,但设置值不保存
我正在使用spring配置的hibernate应用程序。有transactionmanagement和一个定义为entityInterceptor的AuditionInterceptor。调试代码时,我输入entityInterceptors方法并设置日期,但是在保存结束时,它们不在数据库中:( 考虑以下配置Hibernate实体拦截器触发器,但设置值不保存,hibernate,spring-mvc,spring-transactions,Hibernate,Spring Mvc,Spring Transactions,我正在使用spring配置的hibernate应用程序。有transactionmanagement和一个定义为entityInterceptor的AuditionInterceptor。调试代码时,我输入entityInterceptors方法并设置日期,但是在保存结束时,它们不在数据库中:( 考虑以下配置 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.show_sql=${hibernate.show_sql}
hbm2ddl.auto=${hbm2ddl.auto}
</value>
</property>
<property name="schemaUpdate">
<value>true</value>
</property>
<property name="annotatedClasses">
<list>
.. bunch of annotatedClasses" ...
</list>
</property>
</bean>
<bean name="auditInterceptor" class="com.mbalogos.mba.dao.AuditInterceptor" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="entityInterceptor" ref="auditInterceptor"/>
</bean>
<bean id="namedQueryDao" class="com.mbalogos.mba.dao.NamedQueryDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
在save方法之后,sessionFactory被注入到类中
public <T extends DomainObject> T save(T objectToSave) {
Session currentSession = null;
try {
currentSession = sessionFactory.getCurrentSession();
currentSession.save(objectToSave);
return objectToSave;
} catch (Exception ex) {
logger.error(ex);
}
return null;
}
public T保存(T objectToSave){
会话currentSession=null;
试一试{
currentSession=sessionFactory.getCurrentSession();
currentSession.save(objectToSave);
返回objectToSave;
}捕获(例外情况除外){
记录器错误(ex);
}
返回null;
}
任何人都知道为什么会发生这种行为。哦,我还尝试将entityInterceptor放在sessionFactory中,而不是我第一次尝试的transactionmanager中,同样的行为:(我设法弄明白了,我必须处理propertynames及其状态,而不是实体对象……奇怪的是,如果不能处理,为什么要提供实体对象:(
@覆盖
公共布尔onFlushDirty(对象实体,可序列化id,
对象[]当前状态,对象[]先前状态,
字符串[]属性名称,类型[]类型){
返回审核(currentState、propertyNames);
}
@凌驾
公共布尔onSave(对象实体、可序列化id、对象[]状态、,
字符串[]属性名称,类型[]类型){
返回审核(状态、属性名称);
}
私有布尔审核(对象[]当前状态,字符串[]属性名称){
布尔值=假;
Timestamp Timestamp=新时间戳(new Date().getTime());
对于(int i=0;i谢谢Kenny,我也面临着同样的问题。在我的例子中,拦截器对某些实体有效,而对其他实体无效。
一些可能的优化可能是:
*如果已完成两个属性的搜索,则中断循环。
*如果只想对DomainObject应用审核方法,请使用If(DomainObject的实体实例)
我仍然很好奇,为什么直接在实体对象上设置属性对某些实体不起作用。如果您或任何人知道原因,请在这里发布
public <T extends DomainObject> T save(T objectToSave) {
Session currentSession = null;
try {
currentSession = sessionFactory.getCurrentSession();
currentSession.save(objectToSave);
return objectToSave;
} catch (Exception ex) {
logger.error(ex);
}
return null;
}
@Override
public boolean onFlushDirty(Object entity, Serializable id,
Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) {
return audit(currentState, propertyNames);
}
@Override
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
return audit(state, propertyNames);
}
private boolean audit(Object[] currentState, String[] propertyNames) {
boolean changed = false;
Timestamp timestamp = new Timestamp(new Date().getTime());
for(int i=0;i<propertyNames.length;i++){
if("creationDate".equals(propertyNames[i])){
Object currentDate = currentState[i];
if(currentDate == null){
currentState[i] = timestamp;
changed = true;
}
}
if("modificationDate".equals(propertyNames[i])){
currentState[i] = timestamp;
changed = true;
}
}
return changed;
}