Hibernate: MyInterceptor#onFlushDirty is never called
Question: Why MyInterceptor#onFlushDirty
is never called?
I extend AbstractEntityManagerFactoryBean
in xml configs like
<bean id="myEntityManagerFactory" parent="abstractEntityManagerFactoryBean" abstract="true">
<property name="entityInterceptor">
<bean class="xxxx.MyInterceptor"/>
</property>
</bean>
<bean id="abstractEntityManagerFactoryBean" class="xxxx.MyEntityManagerFactoryBean"/>
MyEntityManagerFactoryBean
public class MyEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean implements LoadTimeWeaverAware {
private Interceptor entityInterceptor;
public Interceptor getEntityInterceptor() {
return entityInterceptor;
}
public void setEntityInterceptor(Interceptor interceptor) {
entityInterceptor = interceptor;
}
}
MyInterceptor:
public class MyInterceptor extends EmptyInterceptor {
public MyInterceptor() {
System.out.println("init"); // Works well
}
// PROBLEM - is never called
@Override
public boolean onFlushDirty(Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types) {
if (entity instanceof File) {
.....
}
return false;
}
}
UPDATE: [explanation why custom dirty policy looks like not my way]
I want update modified
timestamp each time I change something in Folder
entity EXCEPT folderPosition
. In the same time folderPosition
should be persistent and not transient (means cause entity to be dirty).
Due I use Spring Transactional and Hibernate Templates, there is some nuances:
1) I can't update modified timestamp at the end of each setter like:
public void setXXX(XXX xxx) {
//PROBLEM: Hibernate templates collect object via setters,
//means simple get query will cause multiple 'modified' timestamp updates
this.xxx = xxx;
this.modified = new Date();
}
2) I can't call setModified
manually, because it has about 25 fields, and setXXX
for each field is scattered across whole app. And I have no power to make refactoring.
@Entity
public class Folder {
/**
* GOAL: Changing of each of these fields except 'folderPosition' should cause
* 'modified' timestamp update
*/
private long id;
private String name;
private Date created;
private Date modified;
private Integer folderLocation;
@PreUpdate
public void preUpdate() {
//PROBLEM : change modified even if only location field has been changed!
//PROBLEM: need to know which fields have been updated!
modified = new Date();
}
....
}
You need to extend the findDirty
method not onFlushDirty
. Check this tutorial for a detail explanation with a reference to a GitHub working example.