mercredi 25 mars 2015

Hibernate EmptyInterceptor not reflected entity updated value in database

I am using spring transaction+hibernate. In debug, EmptyInterceptor's save method called and entity2 property change but changed value not updated in database. Did i forgot something for update value in databse


context file configuration for session factory



<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" autowire="byName">
<property name="packagesToScan">
<list>
<value>com.fg.dda.entity</value>
</list>
</property>
<property name="entityInterceptor">
<bean class="com.fg.dda.interceptor.AuditInterceptor"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.default_schema">dbo</prop>
</props>
</property>
</bean>


class



public class AuditInterceptor extends EmptyInterceptor {

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
if(entity instanceof BaseEntity){
BaseEntity entity2 = (BaseEntity) entity;
CurrentUserDto currentUserDto =(CurrentUserDto)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
entity2.setModifiedBy(currentUserDto.getId());
entity2.setModifiedOn(new Date());
}
return true;
}

@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if(entity instanceof BaseEntity){
BaseEntity entity2 = (BaseEntity) entity;
CurrentUserDto currentUserDto =(CurrentUserDto)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
entity2.setCreatedBy(currentUserDto.getId());
entity2.setCreatedOn(new Date());
entity2.setModifiedBy(currentUserDto.getId());
entity2.setModifiedOn(new Date());
}
return true;
}

}

Aucun commentaire:

Enregistrer un commentaire