Actually, coming back to this: Due to the infinite loop problem I'm having in connection with calling merge(), I'm trying to use update(). Since it might be a problem with the object being detached, I am ensuring that I call save() on the object after I load it from the database, which shouldn't be necessary, right? I'm loading it using the following code:
Code:
public <E extends AbstractPersistentEntity<?>> E getByMultipleFieldValues(
Class<E> returnType,
String[] fieldNames,
Object[] values) {
Session session = getCurrentSession();
try {
Criteria cri = session.createCriteria(returnType);
for (int i=0; i<fieldNames.length; i++) {
if (values[i] == null) cri.add(Restrictions.isNull(fieldNames[i]));
else cri.add(Restrictions.eq(fieldNames[i], values[i]));
}
E ape = (E) cri.uniqueResult();
if (ape != null)
ape.setPersisted(true);
return ape;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
The returned object ought to be attached to the session, yes? I then update it with new values and call this code:
Code:
public <E extends AbstractPersistentEntity<?>> void saveAll(Set<E> set, boolean alreadyPersisted) {
Session session = getCurrentSession();
Transaction t = session.beginTransaction();
for (AbstractPersistentEntity<?> pe : set) {
try {
if (alreadyPersisted)
session.update(pe);
else
session.save(pe);
} catch (NonUniqueObjectException nuoe) {
System.out.println("NUOE: " + nuoe.getIdentifier());
session.merge(pe);
}
}
try {
t.commit();
} catch (Exception e) {
t.rollback();
e.printStackTrace();
}
}
I walked through and made sure that "alreadyPersisted" was true, thus session.update() was getting called. However, as I was walking through the Hibernate code, it seemed to thing that the object was not attached to the session, so the history was lost, and thus when my HistoryInterceptor's onFlushDirty() method was called, the previousState passed to it was null. At the very least now, I have some more insight into the working of the code, but not much more insight into the nature of the problem.