Search in sources :

Example 16 with StrIntTestEntity

use of org.hibernate.envers.test.entities.StrIntTestEntity in project hibernate-orm by hibernate.

the class MaximalizePropertyQuery method testAllLatestRevisionsOfEntityType.

@Test
@TestForIssue(jiraKey = "HHH-7827")
public void testAllLatestRevisionsOfEntityType() {
    List result = getAuditReader().createQuery().forRevisionsOfEntity(StrIntTestEntity.class, false, true).add(AuditEntity.revisionNumber().maximize().computeAggregationInInstanceContext()).addOrder(AuditEntity.property("id").asc()).getResultList();
    Assert.assertEquals(4, result.size());
    Object[] result1 = (Object[]) result.get(0);
    Object[] result2 = (Object[]) result.get(1);
    Object[] result3 = (Object[]) result.get(2);
    Object[] result4 = (Object[]) result.get(3);
    checkRevisionData(result1, 4, RevisionType.MOD, new StrIntTestEntity("d", 5, id1));
    checkRevisionData(result2, 4, RevisionType.MOD, new StrIntTestEntity("a", 20, id2));
    checkRevisionData(result3, 1, RevisionType.ADD, new StrIntTestEntity("c", 42, id3));
    checkRevisionData(result4, 5, RevisionType.DEL, new StrIntTestEntity(null, null, id4));
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) List(java.util.List) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 17 with StrIntTestEntity

use of org.hibernate.envers.test.entities.StrIntTestEntity in project hibernate-orm by hibernate.

the class MaximalizePropertyQuery method testMaximizeInDisjunction.

@Test
@TestForIssue(jiraKey = "HHH-7800")
public void testMaximizeInDisjunction() {
    List<Integer> idsToQuery = Arrays.asList(id1, id3);
    AuditDisjunction disjunction = AuditEntity.disjunction();
    for (Integer id : idsToQuery) {
        disjunction.add(AuditEntity.revisionNumber().maximize().add(AuditEntity.id().eq(id)));
    }
    List result = getAuditReader().createQuery().forRevisionsOfEntity(StrIntTestEntity.class, true, true).add(disjunction).getResultList();
    Set<Integer> idsSeen = new HashSet<Integer>();
    for (Object o : result) {
        StrIntTestEntity entity = (StrIntTestEntity) o;
        Integer id = entity.getId();
        Assert.assertTrue("Entity with ID " + id + " returned but not queried for.", idsToQuery.contains(id));
        if (!idsSeen.add(id)) {
            Assert.fail("Multiple revisions returned with ID " + id + "; expected only one.");
        }
    }
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) AuditDisjunction(org.hibernate.envers.query.criteria.AuditDisjunction) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 18 with StrIntTestEntity

use of org.hibernate.envers.test.entities.StrIntTestEntity in project hibernate-orm by hibernate.

the class RevisionConstraintQuery method testRevisionTypeEqQuery.

@Test
public void testRevisionTypeEqQuery() {
    // The query shouldn't be ordered as always, otherwise - we get an exception.
    List results = getAuditReader().createQuery().forRevisionsOfEntity(StrIntTestEntity.class, true, true).add(AuditEntity.id().eq(id1)).add(AuditEntity.revisionType().eq(RevisionType.MOD)).getResultList();
    Assert.assertEquals(3, results.size());
    Assert.assertEquals(new StrIntTestEntity("d", 10, id1), results.get(0));
    Assert.assertEquals(new StrIntTestEntity("d", 1, id1), results.get(1));
    Assert.assertEquals(new StrIntTestEntity("d", 5, id1), results.get(2));
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) List(java.util.List) Test(org.junit.Test)

Example 19 with StrIntTestEntity

use of org.hibernate.envers.test.entities.StrIntTestEntity in project hibernate-orm by hibernate.

the class RevisionConstraintQuery method initData.

@Test
@Priority(10)
public void initData() {
    // Revision 1
    EntityManager em = getEntityManager();
    em.getTransaction().begin();
    StrIntTestEntity site1 = new StrIntTestEntity("a", 10);
    StrIntTestEntity site2 = new StrIntTestEntity("b", 15);
    em.persist(site1);
    em.persist(site2);
    id1 = site1.getId();
    Integer id2 = site2.getId();
    em.getTransaction().commit();
    // Revision 2
    em.getTransaction().begin();
    site1 = em.find(StrIntTestEntity.class, id1);
    site2 = em.find(StrIntTestEntity.class, id2);
    site1.setStr1("d");
    site2.setNumber(20);
    em.getTransaction().commit();
    // Revision 3
    em.getTransaction().begin();
    site1 = em.find(StrIntTestEntity.class, id1);
    site2 = em.find(StrIntTestEntity.class, id2);
    site1.setNumber(1);
    site2.setStr1("z");
    em.getTransaction().commit();
    // Revision 4
    em.getTransaction().begin();
    site1 = em.find(StrIntTestEntity.class, id1);
    site2 = em.find(StrIntTestEntity.class, id2);
    site1.setNumber(5);
    site2.setStr1("a");
    em.getTransaction().commit();
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) Priority(org.hibernate.envers.test.Priority)

Example 20 with StrIntTestEntity

use of org.hibernate.envers.test.entities.StrIntTestEntity in project hibernate-orm by hibernate.

the class RevisionConstraintQuery method testRevisionTypeNeQuery.

@Test
public void testRevisionTypeNeQuery() {
    // The query shouldn't be ordered as always, otherwise - we get an exception.
    List results = getAuditReader().createQuery().forRevisionsOfEntity(StrIntTestEntity.class, true, true).add(AuditEntity.id().eq(id1)).add(AuditEntity.revisionType().ne(RevisionType.MOD)).getResultList();
    Assert.assertEquals(1, results.size());
    Assert.assertEquals(new StrIntTestEntity("a", 10, id1), results.get(0));
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) List(java.util.List) Test(org.junit.Test)

Aggregations

StrIntTestEntity (org.hibernate.envers.test.entities.StrIntTestEntity)36 Test (org.junit.Test)36 List (java.util.List)14 EntityManager (javax.persistence.EntityManager)12 Priority (org.hibernate.envers.test.Priority)12 StrTestEntity (org.hibernate.envers.test.entities.StrTestEntity)8 TestForIssue (org.hibernate.testing.TestForIssue)7 RevisionType (org.hibernate.envers.RevisionType)6 HashSet (java.util.HashSet)3 AuditDisjunction (org.hibernate.envers.query.criteria.AuditDisjunction)2 AuditCriterion (org.hibernate.envers.query.criteria.AuditCriterion)1 EmbId (org.hibernate.envers.test.entities.ids.EmbId)1 EmbIdTestEntity (org.hibernate.envers.test.entities.ids.EmbIdTestEntity)1 MulId (org.hibernate.envers.test.entities.ids.MulId)1 MulIdTestEntity (org.hibernate.envers.test.entities.ids.MulIdTestEntity)1 CollectionRefEdEntity (org.hibernate.envers.test.entities.onetomany.CollectionRefEdEntity)1 CollectionRefIngEntity (org.hibernate.envers.test.entities.onetomany.CollectionRefIngEntity)1 SetRefIngEmbIdEntity (org.hibernate.envers.test.entities.onetomany.ids.SetRefIngEmbIdEntity)1