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