use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.
the class TestConsole method printPersonHistory.
private void printPersonHistory(StringBuilder sb, int personId) {
AuditReader reader = AuditReaderFactory.get(entityManager);
List personHistory = reader.createQuery().forRevisionsOfEntity(Person.class, false, true).add(AuditEntity.id().eq(personId)).getResultList();
if (personHistory.size() == 0) {
sb.append("A person with id ").append(personId).append(" does not exist.\n");
} else {
for (Object historyObj : personHistory) {
Object[] history = (Object[]) historyObj;
DefaultRevisionEntity revision = (DefaultRevisionEntity) history[1];
sb.append("revision = ").append(revision.getId()).append(", ");
printPerson(sb, (Person) history[0]);
sb.append(" (").append(revision.getRevisionDate()).append(")\n");
}
}
}
use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.
the class AssociationToOneInnerJoinQueryTest method testDisjunctionOfPropertiesFromDifferentEntities.
@Test
public void testDisjunctionOfPropertiesFromDifferentEntities() {
AuditReader auditReader = getAuditReader();
// all cars where the owner has an age of 20 or lives in an address with number 30.
List<Car> resultList = auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.INNER, "p").traverseRelation("address", JoinType.INNER, "a").up().up().add(AuditEntity.disjunction().add(AuditEntity.property("p", "age").eq(20)).add(AuditEntity.property("a", "number").eq(30))).addOrder(AuditEntity.property("make").asc()).getResultList();
assertEquals("Expected two cars to be returned, Toyota and VW", 2, resultList.size());
assertEquals("Unexpected car at index 0", toyota.getId(), resultList.get(0).getId());
assertEquals("Unexpected car at index 1", vw.getId(), resultList.get(1).getId());
}
use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.
the class AssociationToOneLeftJoinQueryTest method testLeftJoinOnAuditedEntity.
@Test
public void testLeftJoinOnAuditedEntity() {
final AuditReader auditReader = getAuditReader();
// all cars where the owner has an age of 20 or where there is no owner at all
List<Car> resultList = auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.LEFT, "p").up().add(AuditEntity.or(AuditEntity.property("p", "age").eq(20), AuditEntity.relatedId("owner").eq(null))).addOrder(AuditEntity.property("make").asc()).getResultList();
assertEquals("The result list should have 2 results, car1 because its owner has an age of 30 and car3 because it has no owner at all", 2, resultList.size());
Car car0 = resultList.get(0);
Car car1 = resultList.get(1);
assertEquals("Unexpected car at index 0", car2.getId(), car0.getId());
assertEquals("Unexpected car at index 0", car3.getId(), car1.getId());
}
use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.
the class AssociationToOneLeftJoinQueryTest method testEntitiesWithANullRelatedIdAreNotReturnedMoreThanOnce.
/**
* In a first attempt to implement left joins in Envers, a full join
* has been performed and than the entities has been filtered in the
* where clause. However, this approach did only work for inner joins
* but not for left joins. One of the defects in this approach is,
* that audit entities, which have a null 'relatedId' and do match
* the query criterias, have been returned multiple times by a query.
* This test ensures that this defect is no longer in the current implementation.
*/
@Test
public void testEntitiesWithANullRelatedIdAreNotReturnedMoreThanOnce() {
final AuditReader auditReader = getAuditReader();
List<Car> resultList = auditReader.createQuery().forEntitiesAtRevision(Car.class, 1).traverseRelation("owner", JoinType.LEFT, "p").up().add(AuditEntity.or(AuditEntity.property("make").eq("car3"), AuditEntity.property("p", "age").eq(10))).getResultList();
assertEquals("Expected car3 to be returned but only once", 1, resultList.size());
assertEquals("Unexpected car at index 0", car3.getId(), resultList.get(0).getId());
}
use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.
the class ListHashcodeChangeTest 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();
}
}
Aggregations