use of com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity in project cuba by cuba-platform.
the class CascadeDeleteTest method testRemoveCascade.
@Test
public void testRemoveCascade() throws Exception {
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
CascadeEntity loadedRoot = em.find(CascadeEntity.class, root.getId());
em.find(CascadeEntity.class, first.getId());
em.remove(loadedRoot);
tx.commit();
}
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
List r = em.createQuery("select e from test$CascadeEntity e").getResultList();
assertEquals(0, r.size());
tx.commit();
}
}
use of com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity in project cuba by cuba-platform.
the class NonDetachedTest method testRecursiveObjects.
@Test
public void testRecursiveObjects() throws Exception {
CascadeEntity e1 = metadata.create(CascadeEntity.class);
e1.setName("cascadeEntity1");
CascadeEntity e2 = metadata.create(CascadeEntity.class);
e2.setName("cascadeEntity2");
e2.setFather(cascadeEntity1);
e1.setChildren(new HashSet<>());
e1.getChildren().add(e2);
e1.setFirstChild(e2);
AppBeans.get(EntityStates.class).makePatch(e1);
CascadeEntity mergedEntity = persistence.callInTransaction(em -> {
return em.merge(e1);
});
assertEquals(cascadeEntity1.getName(), mergedEntity.getName());
}
use of com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity in project cuba by cuba-platform.
the class CascadeDeleteTest method testEntityListenerOnCascadeDelete.
@Test
public void testEntityListenerOnCascadeDelete() throws Exception {
EntityListenerManager entityListenerManager = AppBeans.get(EntityListenerManager.class);
entityListenerManager.addListener(CascadeEntity.class, DeleteCascadeEntityListener.class);
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
CascadeEntity loadedSecond = em.find(CascadeEntity.class, second.getId());
em.find(CascadeEntity.class, third.getId());
em.remove(loadedSecond);
tx.commit();
}
entityListenerManager.removeListener(CascadeEntity.class, DeleteCascadeEntityListener.class);
assertEquals(2, DeleteCascadeEntityListener.deletedEvents.size());
assertTrue(DeleteCascadeEntityListener.deletedEvents.contains(second.getId().toString()));
assertTrue(DeleteCascadeEntityListener.deletedEvents.contains(third.getId().toString()));
}
use of com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity in project cuba by cuba-platform.
the class CascadeDeleteTest method testEntityListenerOnUpdate.
@Test
public void testEntityListenerOnUpdate() throws Exception {
EntityListenerManager entityListenerManager = AppBeans.get(EntityListenerManager.class);
entityListenerManager.addListener(CascadeEntity.class, UpdateCascadeEntityListener.class);
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
CascadeEntity loadedThird = em.find(CascadeEntity.class, third.getId());
CascadeEntity loadedSecond = em.find(CascadeEntity.class, second.getId());
loadedThird.setName("third#1");
tx.commit();
}
entityListenerManager.removeListener(CascadeEntity.class, UpdateCascadeEntityListener.class);
assertEquals(2, UpdateCascadeEntityListener.updatedEvents.size());
assertTrue(UpdateCascadeEntityListener.updatedEvents.contains(second.getId().toString()));
assertTrue(UpdateCascadeEntityListener.updatedEvents.contains(third.getId().toString()));
}
Aggregations