Search in sources :

Example 86 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class EntityLogTest method test.

@Test
public void test() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group group = em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93"));
        User user = new User();
        userId = user.getId();
        user.setGroup(group);
        user.setLogin("test");
        user.setName("test-name");
        em.persist(user);
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        user.setEmail("test-email");
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        user.setName("test-name-1");
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        user.setEmail("test-email-1");
        tx.commit();
    } finally {
        tx.end();
    }
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        em.remove(user);
        tx.commit();
    } finally {
        tx.end();
    }
    List<EntityLogItem> items;
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        TypedQuery<EntityLogItem> query = em.createQuery("select i from sec$EntityLog i where i.entity = ?1 and i.entityRef.entityId = ?2", EntityLogItem.class);
        query.setParameter(1, "sec$User");
        query.setParameter(2, userId);
        items = query.getResultList();
        tx.commit();
    } finally {
        tx.end();
    }
    assertNotNull(items);
    assertEquals(4, items.size());
    assertNotNull(items.get(0).getAttributes());
    assertEquals(1, items.get(0).getAttributes().size());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Test(org.junit.jupiter.api.Test)

Example 87 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class EntityLogTest method testMultipleFlush.

@Test
public void testMultipleFlush() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group group = em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93"));
        User user = new User();
        userId = user.getId();
        user.setGroup(group);
        user.setLogin("test");
        user.setName("test-name");
        user.setEmail("name@test.com");
        em.persist(user);
        em.flush();
        user.setEmail("changed-name@test.com");
        tx.commit();
    } finally {
        tx.end();
    }
    List<EntityLogItem> items = getEntityLogItems();
    assertNotNull(items);
    assertEquals(1, items.size());
    EntityLogItem item = items.get(0);
    assertEquals(EntityLogItem.Type.CREATE, item.getType());
    EntityLogAttr attr = Iterables.find(item.getAttributes(), new Predicate<EntityLogAttr>() {

        @Override
        public boolean apply(EntityLogAttr attr) {
            return "email".equals(attr.getName());
        }
    });
    assertEquals("changed-name@test.com", attr.getValue());
    // //////
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        user.setEmail("changed-2@test.com");
        em.flush();
        user.setEmail("changed-3@test.com");
        tx.commit();
    } finally {
        tx.end();
    }
    items = getEntityLogItems();
    assertNotNull(items);
    assertEquals(2, items.size());
    // the last because of sorting in query
    item = items.get(0);
    assertEquals(EntityLogItem.Type.MODIFY, item.getType());
    attr = Iterables.find(item.getAttributes(), new Predicate<EntityLogAttr>() {

        @Override
        public boolean apply(EntityLogAttr attr) {
            return "email".equals(attr.getName());
        }
    });
    assertEquals("changed-3@test.com", attr.getValue());
    assertEquals("changed-name@test.com", attr.getOldValue());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Predicate(com.google.common.base.Predicate) Test(org.junit.jupiter.api.Test)

Example 88 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class SeveralFetchGroupsTest method setUp.

@BeforeEach
public void setUp() {
    metadata = cont.metadata();
    persistence = cont.persistence();
    dataManager = AppBeans.get(DataManager.class);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        SeveralFetchGroups_Tariff tariff1 = metadata.create(SeveralFetchGroups_Tariff.class);
        tariffId1 = tariff1.getId();
        tariff1.setName("tariff1");
        em.persist(tariff1);
        SeveralFetchGroups_Tariff tariff2_1 = metadata.create(SeveralFetchGroups_Tariff.class);
        tariffId2_1 = tariff2_1.getId();
        tariff2_1.setName("tariff2_1");
        tariff2_1.setParent(tariff1);
        em.persist(tariff2_1);
        SeveralFetchGroups_Tariff tariff3_1 = metadata.create(SeveralFetchGroups_Tariff.class);
        tariffId3_1 = tariff3_1.getId();
        tariff3_1.setName("tariff3_1");
        tariff3_1.setParent(tariff1);
        em.persist(tariff3_1);
        SeveralFetchGroups_Tariff tariff4_2 = metadata.create(SeveralFetchGroups_Tariff.class);
        tariffId4_2 = tariff4_2.getId();
        tariff4_2.setName("tariff4");
        tariff4_2.setParent(tariff2_1);
        em.persist(tariff4_2);
        SeveralFetchGroups_TariffVersion tariffVersion1 = metadata.create(SeveralFetchGroups_TariffVersion.class);
        tariffVersionId1 = tariffVersion1.getId();
        tariffVersion1.setName("1");
        tariffVersion1.setDescription("tariffVersionDescription1");
        tariffVersion1.setParent(tariff1);
        tariff1.setActiveVersion(tariffVersion1);
        em.persist(tariffVersion1);
        SeveralFetchGroups_TariffVersion tariffVersion2 = metadata.create(SeveralFetchGroups_TariffVersion.class);
        tariffVersionId2 = tariffVersion2.getId();
        tariffVersion2.setName("2");
        tariffVersion2.setDescription("tariffVersionDescription2");
        tariffVersion2.setParent(tariff4_2);
        tariff4_2.setActiveVersion(tariffVersion2);
        em.persist(tariffVersion2);
        SeveralFetchGroups_TariffVersion tariffVersion3 = metadata.create(SeveralFetchGroups_TariffVersion.class);
        tariffVersionId3 = tariffVersion3.getId();
        tariffVersion3.setName("3");
        tariffVersion3.setDescription("tariffVersionDescription3");
        tariffVersion3.setParent(tariff2_1);
        tariff2_1.setActiveVersion(tariffVersion3);
        em.persist(tariffVersion3);
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) SeveralFetchGroups_Tariff(com.haulmont.cuba.testmodel.severalfetchgroups.SeveralFetchGroups_Tariff) Transaction(com.haulmont.cuba.core.Transaction) SeveralFetchGroups_TariffVersion(com.haulmont.cuba.testmodel.severalfetchgroups.SeveralFetchGroups_TariffVersion) DataManager(com.haulmont.cuba.core.global.DataManager)

Example 89 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class SoftDeleteMany2ManyTest method testMany2ManyUnlink.

@Test
public void testMany2ManyUnlink() throws Exception {
    try (Transaction tx = cont.persistence().createTransaction()) {
        Many2ManyA a = cont.entityManager().find(Many2ManyA.class, this.many2ManyA.getId());
        assertNotNull(a);
        assertFalse(a.getCollectionOfB().isEmpty());
        assertEquals(many2ManyB, a.getCollectionOfB().iterator().next());
        tx.commit();
    }
    try (Transaction tx = cont.persistence().createTransaction()) {
        Many2ManyB b = cont.entityManager().find(Many2ManyB.class, this.many2ManyB.getId());
        cont.entityManager().remove(b);
        tx.commit();
    }
    try (Transaction tx = cont.persistence().createTransaction()) {
        Many2ManyA a = cont.entityManager().find(Many2ManyA.class, this.many2ManyA.getId());
        assertNotNull(a);
        assertTrue(a.getCollectionOfB().isEmpty());
        tx.commit();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) Many2ManyB(com.haulmont.cuba.testmodel.many2many.Many2ManyB) Many2ManyA(com.haulmont.cuba.testmodel.many2many.Many2ManyA) Test(org.junit.jupiter.api.Test)

Example 90 with Transaction

use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.

the class SoftDeleteMany2ManyTest method test_PL_3452.

/**
 * @see <a href="https://youtrack.haulmont.com/issue/PL-3452">PL-3452</a>
 */
@Test
public void test_PL_3452() throws Exception {
    Many2ManyA a1;
    Many2ManyA a2;
    Many2ManyB b1;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        a1 = em.find(Many2ManyA.class, this.a1.getId());
        assertNotNull(a1);
        assertEquals(2, a1.getCollectionOfB().size());
        a2 = em.find(Many2ManyA.class, this.a2.getId());
        assertNotNull(a2);
        assertEquals(1, a2.getCollectionOfB().size());
        tx.commitRetaining();
        em = cont.entityManager();
        b1 = em.find(Many2ManyB.class, this.b1.getId());
        em.remove(b1);
        tx.commitRetaining();
        em = cont.entityManager();
        a1 = em.find(Many2ManyA.class, this.a1.getId());
        assertNotNull(a1);
        assertEquals(1, a1.getCollectionOfB().size());
        tx.commit();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Many2ManyB(com.haulmont.cuba.testmodel.many2many.Many2ManyB) Many2ManyA(com.haulmont.cuba.testmodel.many2many.Many2ManyA) Test(org.junit.jupiter.api.Test)

Aggregations

Transaction (com.haulmont.cuba.core.Transaction)226 EntityManager (com.haulmont.cuba.core.EntityManager)142 Test (org.junit.jupiter.api.Test)59 Query (com.haulmont.cuba.core.Query)30 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)27 User (com.haulmont.cuba.security.entity.User)25 View (com.haulmont.cuba.core.global.View)22 BeforeEach (org.junit.jupiter.api.BeforeEach)18 TypedQuery (com.haulmont.cuba.core.TypedQuery)13 Group (com.haulmont.cuba.security.entity.Group)12 List (java.util.List)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 MetaClass (com.haulmont.chile.core.model.MetaClass)7 Entity (com.haulmont.cuba.core.entity.Entity)7 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)7 ChildCachedEntity (com.haulmont.cuba.testmodel.entity_cache.ChildCachedEntity)5 ParentCachedEntity (com.haulmont.cuba.testmodel.entity_cache.ParentCachedEntity)5 UUID (java.util.UUID)5 Nullable (javax.annotation.Nullable)5 EntityManagerFactory (javax.persistence.EntityManagerFactory)5