use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.
the class TestConsole method printAddressHistory.
private void printAddressHistory(StringBuilder sb, int addressId) {
AuditReader reader = AuditReaderFactory.get(entityManager);
List addressHistory = reader.createQuery().forRevisionsOfEntity(Address.class, false, true).add(AuditEntity.id().eq(addressId)).getResultList();
if (addressHistory.size() == 0) {
sb.append("A address with id ").append(addressId).append(" does not exist.\n");
} else {
for (Object historyObj : addressHistory) {
Object[] history = (Object[]) historyObj;
DefaultRevisionEntity revision = (DefaultRevisionEntity) history[1];
sb.append("revision = ").append(revision.getId()).append(", ");
printAddress(sb, (Address) history[0]);
sb.append(" (").append(revision.getRevisionDate()).append(")\n");
}
}
}
use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.
the class EmbeddableWithNoDeclaredDataTest method testEmbeddableThatExtendsMappedSuperclass.
@Test
public void testEmbeddableThatExtendsMappedSuperclass() {
// Reload and Compare Revision
EntityManager em = getEntityManager();
em.getTransaction().begin();
EntityWithEmbeddableWithNoDeclaredData entityLoaded = em.find(EntityWithEmbeddableWithNoDeclaredData.class, id);
AuditReader reader = AuditReaderFactory.get(em);
List<Number> revs = reader.getRevisions(EntityWithEmbeddableWithNoDeclaredData.class, id);
Assert.assertEquals(1, revs.size());
EntityWithEmbeddableWithNoDeclaredData entityRev1 = reader.find(EntityWithEmbeddableWithNoDeclaredData.class, id, revs.get(0));
em.getTransaction().commit();
Assert.assertEquals(entityLoaded.getName(), entityRev1.getName());
// value should be null because there is no data in EmbeddableWithNoDeclaredData
// and the fields in AbstractEmbeddable should not be audited.
Assert.assertNull(entityRev1.getValue());
}
use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.
the class SetHashcodeChangeTest method testAuthorLastRevision.
@Test
public void testAuthorLastRevision() {
// tests that Author has 3 books, Book1, Book2, and Book3.
// where Book1 and Book2 were removed and re-added with the addition of Book3.
EntityManager entityManager = getEntityManager();
try {
final AuditReader reader = getAuditReader();
final List<Number> revisions = reader.getRevisions(Author.class, authorId);
final Number lastRevision = revisions.get(revisions.size() - 1);
final Author author = (Author) reader.createQuery().forEntitiesAtRevision(Author.class, lastRevision).getSingleResult();
assertNotNull(author);
assertEquals(3, author.getBooks().size());
} catch (Exception e) {
if (entityManager.getTransaction().isActive()) {
entityManager.getTransaction().rollback();
}
} finally {
entityManager.close();
}
}
use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.
the class AssociationToOneInnerJoinQueryTest method testComparisonOfTwoPropertiesFromDifferentEntities.
@Test
public void testComparisonOfTwoPropertiesFromDifferentEntities() {
AuditReader auditReader = getAuditReader();
// the car where the owner age is equal to the owner address number.
Car result = (Car) auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.INNER, "p").traverseRelation("address", JoinType.INNER, "a").up().up().add(AuditEntity.property("p", "age").eqProperty("a", "number")).getSingleResult();
assertEquals("Unexpected car returned", toyota.getId(), result.getId());
}
use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.
the class AssociationToOneInnerJoinQueryTest method testAssociationQueryWithProjection.
@Test
public void testAssociationQueryWithProjection() {
AuditReader auditReader = getAuditReader();
List<Integer> list1 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 2).traverseRelation("owner", JoinType.INNER).addProjection(AuditEntity.property("age")).addOrder(AuditEntity.property("age").asc()).getResultList();
assertEquals("Unexpected number of results", 3, list1.size());
assertEquals("Unexpected age at index 0", Integer.valueOf(20), list1.get(0));
assertEquals("Unexpected age at index 0", Integer.valueOf(30), list1.get(1));
assertEquals("Unexpected age at index 0", Integer.valueOf(40), list1.get(2));
List<Address> list2 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 2).traverseRelation("owner", JoinType.INNER).addOrder(AuditEntity.property("age").asc()).traverseRelation("address", JoinType.INNER).addProjection(AuditEntity.selectEntity(false)).getResultList();
assertEquals("Unexpected number of results", 3, list2.size());
assertEquals("Unexpected address at index 0", address1.getId(), list2.get(0).getId());
assertEquals("Unexpected address at index 1", address1.getId(), list2.get(1).getId());
assertEquals("Unexpected address at index 2", address2.getId(), list2.get(2).getId());
List<Address> list3 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 2).traverseRelation("owner", JoinType.INNER).traverseRelation("address", JoinType.INNER).addProjection(AuditEntity.selectEntity(true)).addOrder(AuditEntity.property("number").asc()).getResultList();
assertEquals("Unexpected number of results", 2, list3.size());
assertEquals("Unexpected address at index 0", address1.getId(), list3.get(0).getId());
assertEquals("Unexpected address at index 1", address2.getId(), list3.get(1).getId());
List<Object[]> list4 = auditReader.createQuery().forEntitiesAtRevision(Car.class, 2).traverseRelation("owner", JoinType.INNER).addOrder(AuditEntity.property("age").asc()).addProjection(AuditEntity.selectEntity(false)).traverseRelation("address", JoinType.INNER).addProjection(AuditEntity.property("number")).getResultList();
assertEquals("Unexpected number of results", 3, list4.size());
final Object[] index0 = list4.get(0);
assertEquals("Unexpected owner at index 0", vwOwner.getId(), ((Person) index0[0]).getId());
assertEquals("Unexpected number at index 0", Integer.valueOf(5), index0[1]);
final Object[] index1 = list4.get(1);
assertEquals("Unexpected owner at index 1", fordOwner.getId(), ((Person) index1[0]).getId());
assertEquals("Unexpected number at index 1", Integer.valueOf(5), index1[1]);
final Object[] index2 = list4.get(2);
assertEquals("Unexpected owner at index 2", toyotaOwner.getId(), ((Person) index2[0]).getId());
assertEquals("Unexpected number at index 2", Integer.valueOf(30), index2[1]);
}
Aggregations