Search in sources :

Example 31 with AuditReader

use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.

the class CustomDate method testFindRevision.

@Test
public void testFindRevision() {
    AuditReader vr = getAuditReader();
    long rev1Timestamp = vr.findRevision(CustomDateRevEntity.class, 1).getDateTimestamp().getTime();
    assert rev1Timestamp > timestamp1;
    assert rev1Timestamp <= timestamp2;
    long rev2Timestamp = vr.findRevision(CustomDateRevEntity.class, 2).getDateTimestamp().getTime();
    assert rev2Timestamp > timestamp2;
    assert rev2Timestamp <= timestamp3;
}
Also used : AuditReader(org.hibernate.envers.AuditReader) Test(org.junit.Test)

Example 32 with AuditReader

use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.

the class OsgiIntegrationTest method testNativeEnvers.

@Test
public void testNativeEnvers() throws Exception {
    final ServiceReference sr = bundleContext.getServiceReference(SessionFactory.class.getName());
    final SessionFactory sf = (SessionFactory) bundleContext.getService(sr);
    final Integer adpId;
    Session s = sf.openSession();
    s.getTransaction().begin();
    AuditedDataPoint adp = new AuditedDataPoint("Chris");
    s.persist(adp);
    s.getTransaction().commit();
    adpId = adp.getId();
    s.close();
    s = sf.openSession();
    s.getTransaction().begin();
    adp = s.get(AuditedDataPoint.class, adpId);
    adp.setName("Chris2");
    s.getTransaction().commit();
    s.close();
    s = sf.openSession();
    AuditReader ar = AuditReaderFactory.get(s);
    assertEquals(2, ar.getRevisions(AuditedDataPoint.class, adpId).size());
    AuditedDataPoint rev1 = ar.find(AuditedDataPoint.class, adpId, 1);
    AuditedDataPoint rev2 = ar.find(AuditedDataPoint.class, adpId, 2);
    assertEquals(new AuditedDataPoint(adpId, "Chris"), rev1);
    assertEquals(new AuditedDataPoint(adpId, "Chris2"), rev2);
    s.close();
}
Also used : SessionFactory(org.hibernate.SessionFactory) AuditedDataPoint(org.hibernate.osgi.test.client.AuditedDataPoint) AuditReader(org.hibernate.envers.AuditReader) ServiceReference(org.osgi.framework.ServiceReference) Session(org.hibernate.Session) Test(org.junit.Test)

Example 33 with AuditReader

use of org.hibernate.envers.AuditReader in project hibernate-orm by hibernate.

the class OsgiIntegrationTest method testJpaEnvers.

@Test
public void testJpaEnvers() throws Exception {
    final ServiceReference serviceReference = bundleContext.getServiceReference(PersistenceProvider.class.getName());
    final PersistenceProvider persistenceProvider = (PersistenceProvider) bundleContext.getService(serviceReference);
    final EntityManagerFactory emf = persistenceProvider.createEntityManagerFactory("hibernate-osgi-test", null);
    final Integer adpId;
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    AuditedDataPoint adp = new AuditedDataPoint("Chris");
    em.persist(adp);
    em.getTransaction().commit();
    adpId = adp.getId();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    adp = em.find(AuditedDataPoint.class, adpId);
    adp.setName("Chris2");
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    AuditReader ar = AuditReaderFactory.get(em);
    assertEquals(2, ar.getRevisions(AuditedDataPoint.class, adpId).size());
    AuditedDataPoint rev1 = ar.find(AuditedDataPoint.class, adpId, 1);
    AuditedDataPoint rev2 = ar.find(AuditedDataPoint.class, adpId, 2);
    assertEquals(new AuditedDataPoint(adpId, "Chris"), rev1);
    assertEquals(new AuditedDataPoint(adpId, "Chris2"), rev2);
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) AuditedDataPoint(org.hibernate.osgi.test.client.AuditedDataPoint) PersistenceProvider(javax.persistence.spi.PersistenceProvider) EntityManagerFactory(javax.persistence.EntityManagerFactory) AuditReader(org.hibernate.envers.AuditReader) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 34 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 35 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)

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