Hi All,
I did some POCs with EmptyInterceptor. I am facing different behavior of the same code in onSave & onFlushDirty.
Code:
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
A a = (A) entity;
// B
B b1 = new B();
b1.setA(a);
B b2 = new B();
b2.setA(a);
Set<B> bs = new HashSet<B>(0);
bs.add(b1);
bs.add(b2);
for (int i = 0; i < propertyNames.length; i++) {
if ("bs".equals(propertyNames[i])) {
state[i] = bs;
}
}
return super.onSave(entity, id, state, propertyNames, types);
}
The above code works fine in onSave. However, when i try to do a similar thing in onFlushDirty then it is not happening i.e. I am not able to persist child of the entity A when I persist a using the interceptor.
In the onFlushDirty, I add the child objects on the "currentState". Since I got a class cast exception in onFlushDirty, I tried following but that even did not work, though did not get an exception this time.
Code:
a.getBs().add(b1);
a.getBs().add(b2);
for (int i = 0; i < propertyNames.length; i++) {
if ("bs".equals(propertyNames[i])) {
currentState[i] = a.getBs();
}
}
Am I missing here or is there any other way to do this in which I can just call a interceptor/listener on a particular entity and can do things just before it is being persisted.
Thanks for the help
Regards,
Nitin