Search in sources :

Example 1 with Child1Entity

use of org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity in project hibernate-orm by hibernate.

the class ValidityAuditStrategyRevEndTestCustomRevEnt method initData.

@Test
@Priority(10)
public void initData() {
    EntityManager em = getEntityManager();
    // We need first to modify the columns in the middle (join table) to
    // allow null values. Hbm2ddl doesn't seem
    // to allow this.
    em.getTransaction().begin();
    Session session = (Session) em.getDelegate();
    session.createSQLQuery("DROP TABLE children").executeUpdate();
    session.createSQLQuery("CREATE TABLE children ( parent_id " + getDialect().getTypeName(Types.INTEGER) + ", child1_id " + getDialect().getTypeName(Types.INTEGER) + getDialect().getNullColumnString() + ", child2_id " + getDialect().getTypeName(Types.INTEGER) + getDialect().getNullColumnString() + " )").executeUpdate();
    session.createSQLQuery("DROP TABLE children_AUD").executeUpdate();
    session.createSQLQuery("CREATE TABLE children_AUD ( REV " + getDialect().getTypeName(Types.INTEGER) + " NOT NULL" + ", REVEND " + getDialect().getTypeName(Types.INTEGER) + ", " + revendTimestampColumName + " " + getDialect().getTypeName(Types.TIMESTAMP) + ", REVTYPE " + getDialect().getTypeName(Types.TINYINT) + ", parent_id " + getDialect().getTypeName(Types.INTEGER) + ", child1_id " + getDialect().getTypeName(Types.INTEGER) + getDialect().getNullColumnString() + ", child2_id " + getDialect().getTypeName(Types.INTEGER) + getDialect().getNullColumnString() + " )").executeUpdate();
    em.getTransaction().commit();
    em.clear();
    ParentEntity p1 = new ParentEntity("parent_1");
    ParentEntity p2 = new ParentEntity("parent_2");
    Child1Entity c1_1 = new Child1Entity("child1_1");
    Child1Entity c1_2 = new Child1Entity("child1_2");
    Child2Entity c2_1 = new Child2Entity("child2_1");
    Child2Entity c2_2 = new Child2Entity("child2_2");
    // Revision 1
    em.getTransaction().begin();
    em.persist(p1);
    em.persist(p2);
    em.persist(c1_1);
    em.persist(c1_2);
    em.persist(c2_1);
    em.persist(c2_2);
    em.getTransaction().commit();
    em.clear();
    // Revision 2 - (p1: c1_1, p2: c2_1)
    em.getTransaction().begin();
    p1 = em.find(ParentEntity.class, p1.getId());
    p2 = em.find(ParentEntity.class, p2.getId());
    c1_1 = em.find(Child1Entity.class, c1_1.getId());
    c2_1 = em.find(Child2Entity.class, c2_1.getId());
    p1.getChildren1().add(c1_1);
    p2.getChildren2().add(c2_1);
    em.getTransaction().commit();
    em.clear();
    // Revision 3 - (p1: c1_1, c1_2, c2_2, p2: c1_1, c2_1)
    em.getTransaction().begin();
    p1 = em.find(ParentEntity.class, p1.getId());
    p2 = em.find(ParentEntity.class, p2.getId());
    c1_1 = em.find(Child1Entity.class, c1_1.getId());
    c1_2 = em.find(Child1Entity.class, c1_2.getId());
    c2_2 = em.find(Child2Entity.class, c2_2.getId());
    p1.getChildren1().add(c1_2);
    p1.getChildren2().add(c2_2);
    p2.getChildren1().add(c1_1);
    em.getTransaction().commit();
    em.clear();
    // Revision 4 - (p1: c1_2, c2_2, p2: c1_1, c2_1, c2_2)
    em.getTransaction().begin();
    p1 = em.find(ParentEntity.class, p1.getId());
    p2 = em.find(ParentEntity.class, p2.getId());
    c1_1 = em.find(Child1Entity.class, c1_1.getId());
    c2_2 = em.find(Child2Entity.class, c2_2.getId());
    p1.getChildren1().remove(c1_1);
    p2.getChildren2().add(c2_2);
    em.getTransaction().commit();
    em.clear();
    // Revision 5 - (p1: c2_2, p2: c1_1, c2_1)
    em.getTransaction().begin();
    p1 = em.find(ParentEntity.class, p1.getId());
    p2 = em.find(ParentEntity.class, p2.getId());
    c1_2 = em.find(Child1Entity.class, c1_2.getId());
    c2_2 = em.find(Child2Entity.class, c2_2.getId());
    c2_2.getParents().remove(p2);
    c1_2.getParents().remove(p1);
    em.getTransaction().commit();
    em.clear();
    //
    p1_id = p1.getId();
    p2_id = p2.getId();
    c1_1_id = c1_1.getId();
    c1_2_id = c1_2.getId();
    c2_1_id = c2_1.getId();
    c2_2_id = c2_2.getId();
    Set<Number> revisionNumbers = new HashSet<Number>();
    revisionNumbers.addAll(Arrays.asList(1, 2, 3, 4, 5));
    revisions = getAuditReader().findRevisions(CustomDateRevEntity.class, revisionNumbers);
    assert revisions.size() == 5;
}
Also used : EntityManager(javax.persistence.EntityManager) Child1Entity(org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity) ParentEntity(org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity) Child2Entity(org.hibernate.envers.test.entities.manytomany.sametable.Child2Entity) CustomDateRevEntity(org.hibernate.envers.test.entities.reventity.CustomDateRevEntity) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test) Priority(org.hibernate.envers.test.Priority)

Example 2 with Child1Entity

use of org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity in project hibernate-orm by hibernate.

the class ValidityAuditStrategyRevEndTestCustomRevEnt method testHistoryOfParent1.

@Test
public void testHistoryOfParent1() {
    Child1Entity c1_1 = getEntityManager().find(Child1Entity.class, c1_1_id);
    Child1Entity c1_2 = getEntityManager().find(Child1Entity.class, c1_2_id);
    Child2Entity c2_2 = getEntityManager().find(Child2Entity.class, c2_2_id);
    ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p1_id, 1);
    ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p1_id, 2);
    ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p1_id, 3);
    ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p1_id, 4);
    ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p1_id, 5);
    assert TestTools.checkCollection(rev1.getChildren1());
    assert TestTools.checkCollection(rev2.getChildren1(), c1_1);
    assert TestTools.checkCollection(rev3.getChildren1(), c1_1, c1_2);
    assert TestTools.checkCollection(rev4.getChildren1(), c1_2);
    assert TestTools.checkCollection(rev5.getChildren1());
    assert TestTools.checkCollection(rev1.getChildren2());
    assert TestTools.checkCollection(rev2.getChildren2());
    assert TestTools.checkCollection(rev3.getChildren2(), c2_2);
    assert TestTools.checkCollection(rev4.getChildren2(), c2_2);
    assert TestTools.checkCollection(rev5.getChildren2(), c2_2);
}
Also used : Child1Entity(org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity) ParentEntity(org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity) Child2Entity(org.hibernate.envers.test.entities.manytomany.sametable.Child2Entity) Test(org.junit.Test)

Example 3 with Child1Entity

use of org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity in project hibernate-orm by hibernate.

the class ValidityAuditStrategyRevEndTestCustomRevEnt method testHistoryOfChild1_2.

// TODO: this was disabled?
@Test
public void testHistoryOfChild1_2() {
    ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
    Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_2_id, 1);
    Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_2_id, 2);
    Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_2_id, 3);
    Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_2_id, 4);
    Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_2_id, 5);
    assert TestTools.checkCollection(rev1.getParents());
    assert TestTools.checkCollection(rev2.getParents());
    assert TestTools.checkCollection(rev3.getParents(), p1);
    assert TestTools.checkCollection(rev4.getParents(), p1);
    assert TestTools.checkCollection(rev5.getParents());
}
Also used : Child1Entity(org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity) ParentEntity(org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity) Test(org.junit.Test)

Example 4 with Child1Entity

use of org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity in project hibernate-orm by hibernate.

the class ValidityAuditStrategyRevEndTsTest method testHistoryOfChild1_1.

@Test
public void testHistoryOfChild1_1() {
    ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
    ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
    Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_1_id, 1);
    Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_1_id, 2);
    Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_1_id, 3);
    Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_1_id, 4);
    Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_1_id, 5);
    assert TestTools.checkCollection(rev1.getParents());
    assert TestTools.checkCollection(rev2.getParents(), p1);
    assert TestTools.checkCollection(rev3.getParents(), p1, p2);
    assert TestTools.checkCollection(rev4.getParents(), p2);
    assert TestTools.checkCollection(rev5.getParents(), p2);
}
Also used : Child1Entity(org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity) ParentEntity(org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity) Test(org.junit.Test)

Example 5 with Child1Entity

use of org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity in project hibernate-orm by hibernate.

the class ValidityAuditStrategyRevEndTsTest method initData.

@Test
@Priority(10)
public void initData() {
    EntityManager em = getEntityManager();
    // We need first to modify the columns in the middle (join table) to
    // allow null values. Hbm2ddl doesn't seem
    // to allow this.
    em.getTransaction().begin();
    Session session = (Session) em.getDelegate();
    session.createSQLQuery("DROP TABLE children").executeUpdate();
    session.createSQLQuery("CREATE TABLE children ( parent_id " + getDialect().getTypeName(Types.INTEGER) + ", child1_id " + getDialect().getTypeName(Types.INTEGER) + getDialect().getNullColumnString() + ", child2_id " + getDialect().getTypeName(Types.INTEGER) + getDialect().getNullColumnString() + " )").executeUpdate();
    session.createSQLQuery("DROP TABLE children_AUD").executeUpdate();
    session.createSQLQuery("CREATE TABLE children_AUD ( REV " + getDialect().getTypeName(Types.INTEGER) + " NOT NULL" + ", REVEND " + getDialect().getTypeName(Types.INTEGER) + ", " + revendTimestampColumName + " " + getDialect().getTypeName(Types.TIMESTAMP) + ", REVTYPE " + getDialect().getTypeName(Types.TINYINT) + ", parent_id " + getDialect().getTypeName(Types.INTEGER) + ", child1_id " + getDialect().getTypeName(Types.INTEGER) + getDialect().getNullColumnString() + ", child2_id " + getDialect().getTypeName(Types.INTEGER) + getDialect().getNullColumnString() + " )").executeUpdate();
    em.getTransaction().commit();
    em.clear();
    ParentEntity p1 = new ParentEntity("parent_1");
    ParentEntity p2 = new ParentEntity("parent_2");
    Child1Entity c1_1 = new Child1Entity("child1_1");
    Child1Entity c1_2 = new Child1Entity("child1_2");
    Child2Entity c2_1 = new Child2Entity("child2_1");
    Child2Entity c2_2 = new Child2Entity("child2_2");
    // Revision 1
    em.getTransaction().begin();
    em.persist(p1);
    em.persist(p2);
    em.persist(c1_1);
    em.persist(c1_2);
    em.persist(c2_1);
    em.persist(c2_2);
    em.getTransaction().commit();
    em.clear();
    // Revision 2 - (p1: c1_1, p2: c2_1)
    em.getTransaction().begin();
    p1 = em.find(ParentEntity.class, p1.getId());
    p2 = em.find(ParentEntity.class, p2.getId());
    c1_1 = em.find(Child1Entity.class, c1_1.getId());
    c2_1 = em.find(Child2Entity.class, c2_1.getId());
    p1.getChildren1().add(c1_1);
    p2.getChildren2().add(c2_1);
    em.getTransaction().commit();
    em.clear();
    // Revision 3 - (p1: c1_1, c1_2, c2_2, p2: c1_1, c2_1)
    em.getTransaction().begin();
    p1 = em.find(ParentEntity.class, p1.getId());
    p2 = em.find(ParentEntity.class, p2.getId());
    c1_1 = em.find(Child1Entity.class, c1_1.getId());
    c1_2 = em.find(Child1Entity.class, c1_2.getId());
    c2_2 = em.find(Child2Entity.class, c2_2.getId());
    p1.getChildren1().add(c1_2);
    p1.getChildren2().add(c2_2);
    p2.getChildren1().add(c1_1);
    em.getTransaction().commit();
    em.clear();
    // Revision 4 - (p1: c1_2, c2_2, p2: c1_1, c2_1, c2_2)
    em.getTransaction().begin();
    p1 = em.find(ParentEntity.class, p1.getId());
    p2 = em.find(ParentEntity.class, p2.getId());
    c1_1 = em.find(Child1Entity.class, c1_1.getId());
    c2_2 = em.find(Child2Entity.class, c2_2.getId());
    p1.getChildren1().remove(c1_1);
    p2.getChildren2().add(c2_2);
    em.getTransaction().commit();
    em.clear();
    // Revision 5 - (p1: c2_2, p2: c1_1, c2_1)
    em.getTransaction().begin();
    p1 = em.find(ParentEntity.class, p1.getId());
    p2 = em.find(ParentEntity.class, p2.getId());
    c1_2 = em.find(Child1Entity.class, c1_2.getId());
    c2_2 = em.find(Child2Entity.class, c2_2.getId());
    c2_2.getParents().remove(p2);
    c1_2.getParents().remove(p1);
    em.getTransaction().commit();
    em.clear();
    //
    p1_id = p1.getId();
    p2_id = p2.getId();
    c1_1_id = c1_1.getId();
    c1_2_id = c1_2.getId();
    c2_1_id = c2_1.getId();
    c2_2_id = c2_2.getId();
    Set<Number> revisionNumbers = new HashSet<Number>();
    revisionNumbers.addAll(Arrays.asList(1, 2, 3, 4, 5));
    revisions = getAuditReader().findRevisions(SequenceIdRevisionEntity.class, revisionNumbers);
    assert revisions.size() == 5;
}
Also used : EntityManager(javax.persistence.EntityManager) Child1Entity(org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity) SequenceIdRevisionEntity(org.hibernate.envers.enhanced.SequenceIdRevisionEntity) ParentEntity(org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity) Child2Entity(org.hibernate.envers.test.entities.manytomany.sametable.Child2Entity) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test) Priority(org.hibernate.envers.test.Priority)

Aggregations

Child1Entity (org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity)15 ParentEntity (org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity)15 Test (org.junit.Test)15 Child2Entity (org.hibernate.envers.test.entities.manytomany.sametable.Child2Entity)9 EntityManager (javax.persistence.EntityManager)3 Session (org.hibernate.Session)3 Priority (org.hibernate.envers.test.Priority)3 HashSet (java.util.HashSet)2 SequenceIdRevisionEntity (org.hibernate.envers.enhanced.SequenceIdRevisionEntity)1 CustomDateRevEntity (org.hibernate.envers.test.entities.reventity.CustomDateRevEntity)1