Search in sources :

Example 1 with CascadeEntity

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();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) List(java.util.List) CascadeEntity(com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity) Test(org.junit.Test)

Example 2 with CascadeEntity

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());
}
Also used : CascadeEntity(com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity) Test(org.junit.Test)

Example 3 with CascadeEntity

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()));
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) EntityListenerManager(com.haulmont.cuba.core.sys.listener.EntityListenerManager) CascadeEntity(com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity) Test(org.junit.Test)

Example 4 with CascadeEntity

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()));
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) EntityListenerManager(com.haulmont.cuba.core.sys.listener.EntityListenerManager) CascadeEntity(com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity) Test(org.junit.Test)

Aggregations

CascadeEntity (com.haulmont.cuba.testmodel.cascadedelete.CascadeEntity)4 Test (org.junit.Test)4 EntityManager (com.haulmont.cuba.core.EntityManager)3 Transaction (com.haulmont.cuba.core.Transaction)3 EntityListenerManager (com.haulmont.cuba.core.sys.listener.EntityListenerManager)2 List (java.util.List)1