use of org.hibernate.envers.test.entities.collection.MultipleCollectionEntity in project hibernate-orm by hibernate.
the class HasChangedDetachedMultipleCollection method initData.
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1 - addition.
em.getTransaction().begin();
MultipleCollectionEntity mce1 = new MultipleCollectionEntity();
mce1.setText("MultipleCollectionEntity-1-1");
// Persisting entity with empty collections.
em.persist(mce1);
em.getTransaction().commit();
mce1Id = mce1.getId();
// Revision 2 - update.
em.getTransaction().begin();
mce1 = em.find(MultipleCollectionEntity.class, mce1.getId());
MultipleCollectionRefEntity1 mcre1 = new MultipleCollectionRefEntity1();
mcre1.setText("MultipleCollectionRefEntity1-1-1");
mcre1.setMultipleCollectionEntity(mce1);
mce1.addRefEntity1(mcre1);
em.persist(mcre1);
mce1 = em.merge(mce1);
em.getTransaction().commit();
mcre1Id = mcre1.getId();
// No changes.
em.getTransaction().begin();
mce1 = em.find(MultipleCollectionEntity.class, mce1.getId());
mce1 = em.merge(mce1);
em.getTransaction().commit();
em.close();
em = getEntityManager();
// Revision 3 - updating detached collection.
em.getTransaction().begin();
mce1.removeRefEntity1(mcre1);
mce1 = em.merge(mce1);
em.getTransaction().commit();
em.close();
em = getEntityManager();
// Revision 4 - updating detached entity, no changes to collection attributes.
em.getTransaction().begin();
mce1.setRefEntities1(new ArrayList<MultipleCollectionRefEntity1>());
mce1.setRefEntities2(new ArrayList<MultipleCollectionRefEntity2>());
mce1.setText("MultipleCollectionEntity-1-2");
mce1 = em.merge(mce1);
em.getTransaction().commit();
em.close();
em = getEntityManager();
// No changes to detached entity (collections were empty beforeQuery).
em.getTransaction().begin();
mce1.setRefEntities1(new ArrayList<MultipleCollectionRefEntity1>());
mce1.setRefEntities2(new ArrayList<MultipleCollectionRefEntity2>());
mce1 = em.merge(mce1);
em.getTransaction().commit();
// Revision 5 - addition.
em.getTransaction().begin();
MultipleCollectionEntity mce2 = new MultipleCollectionEntity();
mce2.setText("MultipleCollectionEntity-2-1");
MultipleCollectionRefEntity2 mcre2 = new MultipleCollectionRefEntity2();
mcre2.setText("MultipleCollectionRefEntity2-1-1");
mcre2.setMultipleCollectionEntity(mce2);
mce2.addRefEntity2(mcre2);
// Cascade persisting related entity.
em.persist(mce2);
em.getTransaction().commit();
mce2Id = mce2.getId();
mcre2Id = mcre2.getId();
em.close();
}
use of org.hibernate.envers.test.entities.collection.MultipleCollectionEntity in project hibernate-orm by hibernate.
the class DetachedMultipleCollectionChangeTest method initData.
@Test
@Priority(10)
public void initData() throws Exception {
EntityManager em;
MultipleCollectionEntity mce;
MultipleCollectionRefEntity1 re1_1, updatedRe1_1, re1_2, re1_3;
MultipleCollectionRefEntity2 re2_1, updatedRe2_1, re2_2, re2_3;
tm.begin();
try {
em = createIsolatedEntityManager();
em.joinTransaction();
mce = new MultipleCollectionEntity();
mce.setText("MultipleCollectionEntity-1");
em.persist(mce);
mceId1 = mce.getId();
} finally {
tryCommit(tm);
}
assertNotNull(mceId1);
tm.begin();
try {
em = createIsolatedEntityManager();
em.joinTransaction();
re1_1 = new MultipleCollectionRefEntity1();
re1_1.setText("MultipleCollectionRefEntity1-1");
re1_1.setMultipleCollectionEntity(mce);
re1_2 = new MultipleCollectionRefEntity1();
re1_2.setText("MultipleCollectionRefEntity1-2");
re1_2.setMultipleCollectionEntity(mce);
mce.addRefEntity1(re1_1);
mce.addRefEntity1(re1_2);
re2_1 = new MultipleCollectionRefEntity2();
re2_1.setText("MultipleCollectionRefEntity2-1");
re2_1.setMultipleCollectionEntity(mce);
re2_2 = new MultipleCollectionRefEntity2();
re2_2.setText("MultipleCollectionRefEntity2-2");
re2_2.setMultipleCollectionEntity(mce);
mce.addRefEntity2(re2_1);
mce.addRefEntity2(re2_2);
mce = em.merge(mce);
} finally {
tryCommit(tm);
}
for (MultipleCollectionRefEntity1 refEnt1 : mce.getRefEntities1()) {
if (refEnt1.equals(re1_1)) {
re1Id1 = refEnt1.getId();
} else if (refEnt1.equals(re1_2)) {
re1Id2 = refEnt1.getId();
} else {
throw new IllegalStateException("unexpected instance");
}
}
for (MultipleCollectionRefEntity2 refEnt2 : mce.getRefEntities2()) {
if (refEnt2.equals(re2_1)) {
re2Id1 = refEnt2.getId();
} else if (refEnt2.equals(re2_2)) {
re2Id2 = refEnt2.getId();
} else {
throw new IllegalStateException("unexpected instance");
}
}
assertNotNull(re1Id1);
assertNotNull(re1Id2);
assertNotNull(re2Id1);
assertNotNull(re2Id2);
tm.begin();
try {
em = createIsolatedEntityManager();
em.joinTransaction();
assertEquals(2, mce.getRefEntities1().size());
mce.removeRefEntity1(re1_2);
assertEquals(1, mce.getRefEntities1().size());
updatedRe1_1 = mce.getRefEntities1().get(0);
assertEquals(re1_1, updatedRe1_1);
updatedRe1_1.setText("MultipleCollectionRefEntity1-1-updated");
re1_3 = new MultipleCollectionRefEntity1();
re1_3.setText("MultipleCollectionRefEntity1-3");
re1_3.setMultipleCollectionEntity(mce);
mce.addRefEntity1(re1_3);
assertEquals(2, mce.getRefEntities1().size());
assertEquals(2, mce.getRefEntities2().size());
mce.removeRefEntity2(re2_2);
assertEquals(1, mce.getRefEntities2().size());
updatedRe2_1 = mce.getRefEntities2().get(0);
assertEquals(re2_1, updatedRe2_1);
updatedRe2_1.setText("MultipleCollectionRefEntity2-1-updated");
re2_3 = new MultipleCollectionRefEntity2();
re2_3.setText("MultipleCollectionRefEntity2-3");
re2_3.setMultipleCollectionEntity(mce);
mce.addRefEntity2(re2_3);
assertEquals(2, mce.getRefEntities2().size());
mce = em.merge(mce);
} finally {
tryCommit(tm);
}
for (MultipleCollectionRefEntity1 adres : mce.getRefEntities1()) {
if (adres.equals(re1_3)) {
re1Id3 = adres.getId();
}
}
for (MultipleCollectionRefEntity2 partner : mce.getRefEntities2()) {
if (partner.equals(re2_3)) {
re2Id3 = partner.getId();
}
}
assertNotNull(re1Id3);
assertNotNull(re2Id3);
}
Aggregations