isDirty() also seems to invoke Interceptor inconsistently. preFlush() and onFlushDirty() are triggered by isDirty(), but onSave() and onDelete() are not. Then when flush() is done, interceptor's onSave() and onDelete() are never called. Is this a bug, or is this what is expected? It makes implementing a history interceptor very difficult.
Code:
public void testIsDirtyDoesFlush4() {
final Set inserted = new HashSet();
final Set deleted = new HashSet();
final Set updated = new HashSet();
final Set postFlush = new HashSet();
final Set preFlush = new HashSet();
Interceptor interceptor = new EmptyInterceptor() {
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
updated.add(entity);
return false;
}
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
deleted.add(entity);
}
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
inserted.add(entity);
return false;
}
public void postFlush(Iterator entities) {
postFlush.add(new Object());
}
public void preFlush(Iterator entities) {
preFlush.add(new Object());
}
};
Session s = sessionFactory().openSession(interceptor);
//s.setFlushMode(FlushMode.COMMIT);
Manuscript m = (Manuscript)s.createQuery("from Manuscript m where accode = ?")
.setParameter(0, "CNR802").uniqueResult();
m.setPageCnt(m.getPageCnt() + 1);
ManContactInfo contactInfo = new ManContactInfo();
contactInfo.setManuscript(m);
ManContactInfo contactInfo2 = new ManContactInfo();
contactInfo2.setManuscript(m);
m.setPageCnt(m.getPageCnt() + 1);
try {
assertTrue(s.isDirty());
assertFalse(inserted.contains(contactInfo));
assertFalse(inserted.contains(contactInfo2)); //not consistent with onFlushDirty
assertTrue(updated.contains(m)); //should onFlushDirty have been called?
assertEquals(1, preFlush.size()); //should preFlush have been called?
assertTrue(postFlush.isEmpty());
inserted.clear();
deleted.clear();
updated.clear();
preFlush.clear();
postFlush.clear();
m.getManContactInfos().remove(contactInfo);
s.flush();
assertFalse(inserted.contains(contactInfo));
assertFalse(inserted.contains(contactInfo2)); //onSave has been lost
assertTrue(updated.contains(m));
assertEquals(1, preFlush.size());
assertEquals(1, postFlush.size());
} finally {
s.close();
}
}