Search in sources :

Example 26 with AuditReader

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

Example 27 with AuditReader

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

Example 28 with AuditReader

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

Example 29 with AuditReader

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

Example 30 with AuditReader

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