Search in sources :

Example 11 with AuditReader

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");
        }
    }
}
Also used : DefaultRevisionEntity(org.hibernate.envers.DefaultRevisionEntity) List(java.util.List) AuditReader(org.hibernate.envers.AuditReader)

Example 12 with AuditReader

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());
}
Also used : Car(org.hibernate.envers.test.integration.query.entities.Car) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Example 13 with AuditReader

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());
}
Also used : Car(org.hibernate.envers.test.integration.query.entities.Car) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Example 14 with AuditReader

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());
}
Also used : Car(org.hibernate.envers.test.integration.query.entities.Car) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Example 15 with AuditReader

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();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Aggregations

AuditReader (org.hibernate.envers.AuditReader)51 Test (org.junit.Test)27 Car (org.hibernate.envers.test.integration.query.entities.Car)8 EntityManager (javax.persistence.EntityManager)6 HashSet (java.util.HashSet)5 ArrayList (java.util.ArrayList)4 AuditQuery (org.hibernate.envers.query.AuditQuery)4 List (java.util.List)2 Map (java.util.Map)2 Query (javax.persistence.Query)2 Session (org.hibernate.Session)2 DefaultRevisionEntity (org.hibernate.envers.DefaultRevisionEntity)2 CustomRevEntity (org.hibernate.envers.test.entities.reventity.CustomRevEntity)2 AuditedDataPoint (org.hibernate.osgi.test.client.AuditedDataPoint)2 ServiceReference (org.osgi.framework.ServiceReference)2 Date (java.util.Date)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 PersistenceProvider (javax.persistence.spi.PersistenceProvider)1 SessionFactory (org.hibernate.SessionFactory)1 CustomPropertyAccessRevEntity (org.hibernate.envers.test.entities.reventity.CustomPropertyAccessRevEntity)1