We've got a requirement: log and persist the entity changes in the transaction in terms of diff using Hibernate Envers. We implemented a RevisionListener
:
public class MyRevisionListener implements EntityTrackingRevisionListener {
@Override
public void newRevision(Object revision) {
...
}
@Override
public void entityChanged(Class entityClass,
String entityName,
Serializable entityId,
RevisionType revisionType,
Object revisionEntity) {
int revisionId = ((DefaultRevisionEntity) revisionEntity).getId();
List<?> revisions = AuditReaderFactory.get(entityManager)
.createQuery()
.forRevisionsOfEntity(entityClass, false, true)
.add(AuditEntity.id().eq(entityId))
.add(AuditEntity.revisionNumber().le(revisionId + 1))
.addOrder(AuditEntity.revisionNumber().desc())
.setMaxResults(2)
.getResultList();
checkArgument(revisions.size() < 3, "Need at most two revisions: %s", revisions);
checkArgument(revisions.size() > 0, "Need at least one revision: %s", revisions);
// continue with diff calculation;
}
}
Questions:
1. First assertion: Do I need this checking? Having the above query Is it possible that the result contains more than two items?
2. Second assertion: I assume for every change (INSERT/UPDATE/DELETE) there's at least one snapshot (or revision). Is that right? If so why do my test cases (with an UPDATE operation) fail randomly due to this assertion failure (meaning there's no snapshot).
Please let me know if I need to provide more information.
UPDATE
The problem was with Spring Test, context, and bean management (specifically how I obtain EntityManager
). I accepted the post by @Naros as it answered the first question and gave a hit for the second question :)