Hi All,
Currently I am using Hibernate 4.0.1 and spring security 3.1.2 in my web application.
I would like to set create by and update by using Interceptor Hibernate
Code:
@Entity
class User {
...
}
@Entity
class Product implement AuditLog {
public User getCreatedUser() {
...
}
}
class AuditLogInterceptor extends EmptyInterceptor {
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames,
Type[] types) {
User user = userDao.getActiveUser();
Date now = DateHelper.getCurrentDateTime();
myentity.setCreatedBy(user);
myentity.setCreatedDate(now);
...
}
}
ApplicationContext.xml
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory"
destroy-method="destroy">
<property name="dataSource" ref="datasource" />
<property name="packagesToScan">
<list>
<value>org.dolphin.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
<property name="entityInterceptor" ref="auditLogInterceptor" />
</bean>
The problem is, in the auditLogInterceptor, I use userDao which need SessionFactory to be injected. So in this case, there is circular dependency. How to solve this issue?