0

I implemented a Hibernate interceptor (extends EmptyInterceptor) and implemented the onFlushDirty method in order to set an object's property to null when that object is saved. The code looks like this:

public boolean onFlushDirty(...) {
// looking for the property index
int i = 0;
for (i=0; i<propertyNames.length; i++) {
  if ("someProperty".equals(propertyNames[i])) {
    break;
  }
}

// setting it to null
currentState[i] = null;

Unfortunatelly, the record is still saved to the database even though i nullified the object. Strangely, when i modify that object, the change is save to the db.

Both the object and the property are entities.

Wojciech Górski
  • 945
  • 11
  • 29

2 Answers2

0

I am pretty sure that the Interceptor is not being invoked in this situation...

Can you please try this while persisting your data...

session.evict(entity);
session.update(entity);

and post the findings. This will force hibernate to call onFlushDirty.

Also put @Override annotation over onFlushDirty to make sure it is properly overridden.

Community
  • 1
  • 1
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • The interceptor is being invoked. The @Override annotation is present and when adding log output to the method, it gets printed out. – Wojciech Górski Aug 19 '12 at 21:17
0

The problem was with the entity being saved to the db even before the interceptor was run. This is because it is an entity and needs to be saved before hibernate can reference it in another entity.

Wojciech Górski
  • 945
  • 11
  • 29