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;
}
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();
}
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();
}
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());
}
Aggregations