use of org.hibernate.envers.test.entities.manytomany.unidirectional.JoinTableEntity in project hibernate-orm by hibernate.
the class JoinTableDetachedTest method initData.
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1 - addition
em.getTransaction().begin();
JoinTableEntity collectionEntity = new JoinTableEntity("some data");
StrTestEntity element1 = new StrTestEntity("str1");
StrTestEntity element2 = new StrTestEntity("str2");
collectionEntity.getReferences().add(element1);
collectionEntity.getReferences().add(element2);
em.persist(element1);
em.persist(element2);
em.persist(collectionEntity);
em.getTransaction().commit();
collectionEntityId = collectionEntity.getId();
element1Id = element1.getId();
element2Id = element2.getId();
em.close();
em = getEntityManager();
// Revision 2 - simple modification
em.getTransaction().begin();
collectionEntity = em.find(JoinTableEntity.class, collectionEntity.getId());
collectionEntity.setData("some other data");
collectionEntity = em.merge(collectionEntity);
em.getTransaction().commit();
em.close();
em = getEntityManager();
// Revision 3 - remove detached object from collection
em.getTransaction().begin();
collectionEntity = em.find(JoinTableEntity.class, collectionEntity.getId());
collectionEntity.getReferences().remove(element1);
collectionEntity = em.merge(collectionEntity);
em.getTransaction().commit();
em.close();
em = getEntityManager();
// Revision 4 - replace the collection
em.getTransaction().begin();
collectionEntity = em.find(JoinTableEntity.class, collectionEntity.getId());
collectionEntity.setReferences(new HashSet<StrTestEntity>());
collectionEntity = em.merge(collectionEntity);
em.getTransaction().commit();
em.close();
em = getEntityManager();
// Revision 5 - add to collection
em.getTransaction().begin();
collectionEntity = em.find(JoinTableEntity.class, collectionEntity.getId());
collectionEntity.getReferences().add(element1);
collectionEntity = em.merge(collectionEntity);
em.getTransaction().commit();
em.close();
}
use of org.hibernate.envers.test.entities.manytomany.unidirectional.JoinTableEntity in project hibernate-orm by hibernate.
the class JoinTableDetachedTest method testHistoryOfCollectionEntity.
@Test
public void testHistoryOfCollectionEntity() {
// Revision 1
JoinTableEntity collectionEntity = new JoinTableEntity(collectionEntityId, "some data");
StrTestEntity element1 = new StrTestEntity("str1", element1Id);
StrTestEntity element2 = new StrTestEntity("str2", element2Id);
collectionEntity.getReferences().add(element1);
collectionEntity.getReferences().add(element2);
JoinTableEntity ver1 = getAuditReader().find(JoinTableEntity.class, collectionEntityId, 1);
Assert.assertEquals(collectionEntity, ver1);
Assert.assertEquals(collectionEntity.getReferences(), ver1.getReferences());
// Revision 2
collectionEntity.setData("some other data");
JoinTableEntity ver2 = getAuditReader().find(JoinTableEntity.class, collectionEntityId, 2);
Assert.assertEquals(collectionEntity, ver2);
Assert.assertEquals(collectionEntity.getReferences(), ver2.getReferences());
// Revision 3
collectionEntity.getReferences().remove(element1);
JoinTableEntity ver3 = getAuditReader().find(JoinTableEntity.class, collectionEntityId, 3);
Assert.assertEquals(collectionEntity, ver3);
Assert.assertEquals(collectionEntity.getReferences(), ver3.getReferences());
// Revision 4
collectionEntity.setReferences(new HashSet<StrTestEntity>());
JoinTableEntity ver4 = getAuditReader().find(JoinTableEntity.class, collectionEntityId, 4);
Assert.assertEquals(collectionEntity, ver4);
Assert.assertEquals(collectionEntity.getReferences(), ver4.getReferences());
// Revision 5
collectionEntity.getReferences().add(element1);
JoinTableEntity ver5 = getAuditReader().find(JoinTableEntity.class, collectionEntityId, 5);
Assert.assertEquals(collectionEntity, ver5);
Assert.assertEquals(collectionEntity.getReferences(), ver5.getReferences());
}
Aggregations