Search in sources :

Example 1 with TargetNotAuditedEntity

use of org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity in project hibernate-orm by hibernate.

the class RelationNotAuditedTarget method initData.

@Test
@Priority(10)
public void initData() {
    EntityManager em = getEntityManager();
    UnversionedStrTestEntity uste1 = new UnversionedStrTestEntity("str1");
    UnversionedStrTestEntity uste2 = new UnversionedStrTestEntity("str2");
    // No revision
    em.getTransaction().begin();
    em.persist(uste1);
    em.persist(uste2);
    em.getTransaction().commit();
    // Revision 1
    em.getTransaction().begin();
    uste1 = em.find(UnversionedStrTestEntity.class, uste1.getId());
    uste2 = em.find(UnversionedStrTestEntity.class, uste2.getId());
    TargetNotAuditedEntity tnae1 = new TargetNotAuditedEntity(1, "tnae1", uste1);
    TargetNotAuditedEntity tnae2 = new TargetNotAuditedEntity(2, "tnae2", uste2);
    em.persist(tnae1);
    em.persist(tnae2);
    em.getTransaction().commit();
    // Revision 2
    em.getTransaction().begin();
    tnae1 = em.find(TargetNotAuditedEntity.class, tnae1.getId());
    tnae2 = em.find(TargetNotAuditedEntity.class, tnae2.getId());
    tnae1.setReference(uste2);
    tnae2.setReference(uste1);
    em.getTransaction().commit();
    // Revision 3
    em.getTransaction().begin();
    tnae1 = em.find(TargetNotAuditedEntity.class, tnae1.getId());
    tnae2 = em.find(TargetNotAuditedEntity.class, tnae2.getId());
    //field not changed!!!
    tnae1.setReference(uste2);
    tnae2.setReference(uste2);
    em.getTransaction().commit();
    // Revision 4
    em.getTransaction().begin();
    tnae1 = em.find(TargetNotAuditedEntity.class, tnae1.getId());
    tnae2 = em.find(TargetNotAuditedEntity.class, tnae2.getId());
    tnae1.setReference(uste1);
    tnae2.setReference(uste1);
    em.getTransaction().commit();
    //
    tnae1_id = tnae1.getId();
    tnae2_id = tnae2.getId();
    uste1_id = uste1.getId();
    uste2_id = uste2.getId();
}
Also used : EntityManager(javax.persistence.EntityManager) UnversionedStrTestEntity(org.hibernate.envers.test.entities.UnversionedStrTestEntity) TargetNotAuditedEntity(org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity) Test(org.junit.Test) Priority(org.hibernate.envers.test.Priority)

Example 2 with TargetNotAuditedEntity

use of org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity in project hibernate-orm by hibernate.

the class ProxyIdentifier method testProxyIdentifier.

@Test
public void testProxyIdentifier() {
    TargetNotAuditedEntity rev1 = getAuditReader().find(TargetNotAuditedEntity.class, tnae1.getId(), 1);
    Assert.assertTrue(rev1.getReference() instanceof HibernateProxy);
    HibernateProxy proxyCreateByEnvers = (HibernateProxy) rev1.getReference();
    LazyInitializer lazyInitializer = proxyCreateByEnvers.getHibernateLazyInitializer();
    Assert.assertTrue(lazyInitializer.isUninitialized());
    Assert.assertNotNull(lazyInitializer.getIdentifier());
    Assert.assertEquals(tnae1.getId(), lazyInitializer.getIdentifier());
    Assert.assertTrue(lazyInitializer.isUninitialized());
    Assert.assertEquals(uste1.getId(), rev1.getReference().getId());
    Assert.assertEquals(uste1.getStr(), rev1.getReference().getStr());
    Assert.assertFalse(lazyInitializer.isUninitialized());
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy) TargetNotAuditedEntity(org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity) Test(org.junit.Test)

Example 3 with TargetNotAuditedEntity

use of org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity in project hibernate-orm by hibernate.

the class ProxyIdentifier method initData.

@Test
@Priority(10)
public void initData() {
    EntityManager em = getEntityManager();
    uste1 = new UnversionedStrTestEntity("str1");
    uste2 = new UnversionedStrTestEntity("str2");
    // No revision
    em.getTransaction().begin();
    em.persist(uste1);
    em.persist(uste2);
    em.getTransaction().commit();
    // Revision 1
    em.getTransaction().begin();
    uste1 = em.find(UnversionedStrTestEntity.class, uste1.getId());
    tnae1 = new TargetNotAuditedEntity(1, "tnae1", uste1);
    em.persist(tnae1);
    em.getTransaction().commit();
    // Revision 2
    em.getTransaction().begin();
    uste2 = em.find(UnversionedStrTestEntity.class, uste2.getId());
    mtonane1 = new ManyToOneNotAuditedNullEntity(2, "mtonane1", uste2);
    mtmnane1 = new ManyToManyNotAuditedNullEntity(3, "mtmnane1");
    mtmnane1.getReferences().add(uste2);
    otmnane1 = new OneToManyNotAuditedNullEntity(4, "otmnane1");
    otmnane1.getReferences().add(uste2);
    emtonane1 = new ExtManyToOneNotAuditedNullEntity(5, "emtonane1", uste2, "extension");
    em.persist(mtonane1);
    em.persist(mtmnane1);
    em.persist(otmnane1);
    em.persist(emtonane1);
    em.getTransaction().commit();
    em.clear();
    // Revision 3
    // Remove not audited target entity, so we can verify null reference
    // when @NotFound(action = NotFoundAction.IGNORE) applied.
    em.getTransaction().begin();
    ManyToOneNotAuditedNullEntity tmp1 = em.find(ManyToOneNotAuditedNullEntity.class, mtonane1.getId());
    tmp1.setReference(null);
    tmp1 = em.merge(tmp1);
    ManyToManyNotAuditedNullEntity tmp2 = em.find(ManyToManyNotAuditedNullEntity.class, mtmnane1.getId());
    tmp2.setReferences(null);
    tmp2 = em.merge(tmp2);
    OneToManyNotAuditedNullEntity tmp3 = em.find(OneToManyNotAuditedNullEntity.class, otmnane1.getId());
    tmp3.setReferences(null);
    tmp3 = em.merge(tmp3);
    ExtManyToOneNotAuditedNullEntity tmp4 = em.find(ExtManyToOneNotAuditedNullEntity.class, emtonane1.getId());
    tmp4.setReference(null);
    tmp4 = em.merge(tmp4);
    em.remove(em.getReference(UnversionedStrTestEntity.class, uste2.getId()));
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) OneToManyNotAuditedNullEntity(org.hibernate.envers.test.entities.onetomany.OneToManyNotAuditedNullEntity) UnversionedStrTestEntity(org.hibernate.envers.test.entities.UnversionedStrTestEntity) ManyToOneNotAuditedNullEntity(org.hibernate.envers.test.entities.manytoone.unidirectional.ManyToOneNotAuditedNullEntity) ExtManyToOneNotAuditedNullEntity(org.hibernate.envers.test.entities.manytoone.unidirectional.ExtManyToOneNotAuditedNullEntity) ExtManyToOneNotAuditedNullEntity(org.hibernate.envers.test.entities.manytoone.unidirectional.ExtManyToOneNotAuditedNullEntity) TargetNotAuditedEntity(org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity) ManyToManyNotAuditedNullEntity(org.hibernate.envers.test.entities.manytomany.unidirectional.ManyToManyNotAuditedNullEntity) Test(org.junit.Test) Priority(org.hibernate.envers.test.Priority)

Example 4 with TargetNotAuditedEntity

use of org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity in project hibernate-orm by hibernate.

the class RelationNotAuditedTarget method testHistoryOfTnae2_id.

@Test
public void testHistoryOfTnae2_id() {
    UnversionedStrTestEntity uste1 = getEntityManager().find(UnversionedStrTestEntity.class, uste1_id);
    UnversionedStrTestEntity uste2 = getEntityManager().find(UnversionedStrTestEntity.class, uste2_id);
    TargetNotAuditedEntity rev1 = getAuditReader().find(TargetNotAuditedEntity.class, tnae2_id, 1);
    TargetNotAuditedEntity rev2 = getAuditReader().find(TargetNotAuditedEntity.class, tnae2_id, 2);
    TargetNotAuditedEntity rev3 = getAuditReader().find(TargetNotAuditedEntity.class, tnae2_id, 3);
    TargetNotAuditedEntity rev4 = getAuditReader().find(TargetNotAuditedEntity.class, tnae2_id, 4);
    assert rev1.getReference().equals(uste2);
    assert rev2.getReference().equals(uste1);
    assert rev3.getReference().equals(uste2);
    assert rev4.getReference().equals(uste1);
}
Also used : UnversionedStrTestEntity(org.hibernate.envers.test.entities.UnversionedStrTestEntity) TargetNotAuditedEntity(org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity) Test(org.junit.Test)

Example 5 with TargetNotAuditedEntity

use of org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity in project hibernate-orm by hibernate.

the class RelationNotAuditedTarget method testHistoryOfTnae1_id.

@Test
public void testHistoryOfTnae1_id() {
    // load original "tnae1" TargetNotAuditedEntity to force load "str1" UnversionedStrTestEntity as Proxy
    TargetNotAuditedEntity original = getEntityManager().find(TargetNotAuditedEntity.class, tnae1_id);
    UnversionedStrTestEntity uste1 = getEntityManager().find(UnversionedStrTestEntity.class, uste1_id);
    UnversionedStrTestEntity uste2 = getEntityManager().find(UnversionedStrTestEntity.class, uste2_id);
    TargetNotAuditedEntity rev1 = getAuditReader().find(TargetNotAuditedEntity.class, tnae1_id, 1);
    TargetNotAuditedEntity rev2 = getAuditReader().find(TargetNotAuditedEntity.class, tnae1_id, 2);
    TargetNotAuditedEntity rev3 = getAuditReader().find(TargetNotAuditedEntity.class, tnae1_id, 3);
    TargetNotAuditedEntity rev4 = getAuditReader().find(TargetNotAuditedEntity.class, tnae1_id, 4);
    assert rev1.getReference().equals(uste1);
    assert rev2.getReference().equals(uste2);
    assert rev3.getReference().equals(uste2);
    assert rev4.getReference().equals(uste1);
    assert original.getReference() instanceof HibernateProxy;
    assert UnversionedStrTestEntity.class.equals(Hibernate.getClass(original.getReference()));
    assert UnversionedStrTestEntity.class.equals(HibernateProxyHelper.getClassWithoutInitializingProxy(rev1.getReference()));
    assert UnversionedStrTestEntity.class.equals(Hibernate.getClass(rev1.getReference()));
}
Also used : UnversionedStrTestEntity(org.hibernate.envers.test.entities.UnversionedStrTestEntity) HibernateProxy(org.hibernate.proxy.HibernateProxy) TargetNotAuditedEntity(org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity) Test(org.junit.Test)

Aggregations

TargetNotAuditedEntity (org.hibernate.envers.test.entities.manytoone.unidirectional.TargetNotAuditedEntity)5 Test (org.junit.Test)5 UnversionedStrTestEntity (org.hibernate.envers.test.entities.UnversionedStrTestEntity)4 EntityManager (javax.persistence.EntityManager)2 Priority (org.hibernate.envers.test.Priority)2 HibernateProxy (org.hibernate.proxy.HibernateProxy)2 ManyToManyNotAuditedNullEntity (org.hibernate.envers.test.entities.manytomany.unidirectional.ManyToManyNotAuditedNullEntity)1 ExtManyToOneNotAuditedNullEntity (org.hibernate.envers.test.entities.manytoone.unidirectional.ExtManyToOneNotAuditedNullEntity)1 ManyToOneNotAuditedNullEntity (org.hibernate.envers.test.entities.manytoone.unidirectional.ManyToOneNotAuditedNullEntity)1 OneToManyNotAuditedNullEntity (org.hibernate.envers.test.entities.onetomany.OneToManyNotAuditedNullEntity)1 LazyInitializer (org.hibernate.proxy.LazyInitializer)1