Search in sources :

Example 96 with Transaction

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

the class SoftDeleteTest method testOneToOne.

@Test
public void testOneToOne() {
    System.out.println("===================== BEGIN testOneToOne =====================");
    // test fetchMode = AUTO
    System.out.println("===================== BEGIN testOneToOne fetchMode = AUTO =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        View view = new View(SoftDeleteOneToOneA.class, "testView").addProperty("name").addProperty("b", new View(SoftDeleteOneToOneB.class, "testView").addProperty("name"));
        SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA2Id, view);
        assertNotNull(oneToOneA);
        assertNotNull(oneToOneA.getB());
        assertEquals(oneToOneA.getB().getId(), oneToOneB2Id);
        tx.commit();
    } finally {
        tx.end();
    }
    // test fetchMode = BATCH
    System.out.println("===================== BEGIN testOneToOneBy fetchMode = BATCH =====================");
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        View view = new View(SoftDeleteOneToOneA.class, "testView").addProperty("name").addProperty("b", new View(SoftDeleteOneToOneB.class, "testView").addProperty("name"), FetchMode.BATCH);
        SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA2Id, view);
        assertNotNull(oneToOneA);
        assertNotNull(oneToOneA.getB());
        assertEquals(oneToOneA.getB().getId(), oneToOneB2Id);
        tx.commit();
    } finally {
        tx.end();
    }
    // test fetchMode = UNDEFINED
    System.out.println("===================== BEGIN testOneToOneBy fetchMode = UNDEFINED =====================");
    tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        View view = new View(SoftDeleteOneToOneA.class, "testView").addProperty("name").addProperty("b", new View(SoftDeleteOneToOneB.class, "testView").addProperty("name"), FetchMode.UNDEFINED);
        SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA2Id, view);
        assertNotNull(oneToOneA);
        assertNotNull(oneToOneA.getB());
        assertEquals(oneToOneA.getB().getId(), oneToOneB2Id);
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToOne =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Transaction(com.haulmont.cuba.core.Transaction) SoftDeleteOneToOneA(com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA) View(com.haulmont.cuba.core.global.View)

Example 97 with Transaction

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

the class SoftDeleteTest method loadDeletedUser.

private void loadDeletedUser() {
    try (Transaction tx = cont.persistence().createTransaction()) {
        User u = cont.entityManager().find(User.class, userId);
        assertNull(u);
        tx.commit();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction)

Example 98 with Transaction

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

the class SoftDeleteTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    persistence = cont.persistence();
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group group = new Group();
        groupId = group.getId();
        group.setName("testGroup");
        em.persist(group);
        User user = new User();
        userId = user.getId();
        user.setName("testUser");
        user.setLogin("testLogin");
        user.setGroup(group);
        em.persist(user);
        User user1 = new User();
        user1Id = user1.getId();
        user1.setName("testUser1");
        user1.setLogin("testLogin1");
        user1.setGroup(group);
        em.persist(user1);
        User user2 = new User();
        user2Id = user2.getId();
        user2.setName("testUser2");
        user2.setLogin("testLogin2");
        user2.setGroup(group);
        em.persist(user2);
        Role role1 = new Role();
        role1Id = role1.getId();
        role1.setName("role1");
        em.persist(role1);
        UserRole userRole1 = new UserRole();
        userRole1Id = userRole1.getId();
        userRole1.setUser(user);
        userRole1.setRole(role1);
        em.persist(userRole1);
        Role role2 = new Role();
        role2Id = role2.getId();
        role2.setName("roleToBeDeleted");
        em.persist(role2);
        Role role3 = new Role();
        role3Id = role3.getId();
        role3.setName("roleToBeDeleted3");
        em.persist(role3);
        Role role4 = new Role();
        role4Id = role4.getId();
        role4.setName("role4");
        em.persist(role4);
        UserRole userRole2 = new UserRole();
        userRole2Id = userRole2.getId();
        userRole2.setUser(user);
        userRole2.setRole(role2);
        em.persist(userRole2);
        UserRole userRole3 = new UserRole();
        userRole3Id = userRole3.getId();
        userRole3.setUser(user1);
        userRole3.setRole(role3);
        em.persist(userRole3);
        UserRole userRole4 = new UserRole();
        userRole4Id = userRole4.getId();
        userRole4.setUser(user2);
        userRole4.setRole(role4);
        em.persist(userRole4);
        SoftDeleteOneToOneB oneToOneB1 = cont.metadata().create(SoftDeleteOneToOneB.class);
        oneToOneB1.setName("oneToOneB1");
        em.persist(oneToOneB1);
        oneToOneB1Id = oneToOneB1.getId();
        SoftDeleteOneToOneB oneToOneB2 = cont.metadata().create(SoftDeleteOneToOneB.class);
        oneToOneB2.setName("oneToOneB2");
        em.persist(oneToOneB2);
        oneToOneB2Id = oneToOneB2.getId();
        SoftDeleteOneToOneA oneToOneA1 = cont.metadata().create(SoftDeleteOneToOneA.class);
        oneToOneA1.setName("oneToOneA1");
        oneToOneA1.setB(oneToOneB1);
        em.persist(oneToOneA1);
        oneToOneA1Id = oneToOneA1.getId();
        SoftDeleteOneToOneA oneToOneA2 = cont.metadata().create(SoftDeleteOneToOneA.class);
        oneToOneA2.setName("oneToOneA2");
        oneToOneA2.setB(oneToOneB2);
        em.persist(oneToOneA2);
        oneToOneA2Id = oneToOneA2.getId();
        Group group1 = new Group();
        group1Id = group1.getId();
        group1.setName("testGroup1");
        em.persist(group1);
        GroupHierarchy groupHierarchy = new GroupHierarchy();
        groupHierarchyId = groupHierarchy.getId();
        groupHierarchy.setGroup(group1);
        groupHierarchy.setParent(group1);
        groupHierarchy.setLevel(1);
        em.persist(groupHierarchy);
        Constraint constraint1 = new Constraint();
        constraint1Id = constraint1.getId();
        constraint1.setCode("constraint1");
        constraint1.setEntityName("sec$Constraint");
        constraint1.setGroup(group1);
        em.persist(constraint1);
        Constraint constraint2 = new Constraint();
        constraint2Id = constraint2.getId();
        constraint2.setCode("constraint2");
        constraint2.setEntityName("sec$Constraint");
        constraint2.setGroup(group1);
        em.persist(constraint2);
        tx.commitRetaining();
        em = cont.persistence().getEntityManager();
        UserRole ur = em.find(UserRole.class, userRole2Id);
        em.remove(ur);
        Role r = em.find(Role.class, role2Id);
        em.remove(r);
        SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA1Id);
        em.remove(oneToOneA);
        SoftDeleteOneToOneB oneToOneB = em.find(SoftDeleteOneToOneB.class, oneToOneB2Id);
        em.remove(oneToOneB);
        Constraint constraint = em.find(Constraint.class, constraint2Id);
        em.remove(constraint);
        // remove from db to prevent cascade delete user role
        QueryRunner queryRunner = new QueryRunner();
        queryRunner.update(em.getConnection(), "update SEC_ROLE set DELETE_TS = CREATE_TS, DELETED_BY = CREATED_BY where name = 'roleToBeDeleted3'");
        tx.commit();
    } finally {
        tx.end();
    }
    QueryRunner queryRunner = new QueryRunner(persistence.getDataSource());
    queryRunner.update("update SEC_USER set DELETE_TS = current_timestamp, DELETED_BY = 'admin' where ID = ?", new Object[] { user2Id.toString() });
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Transaction(com.haulmont.cuba.core.Transaction) SoftDeleteOneToOneA(com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA) SoftDeleteOneToOneB(com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneB) QueryRunner(com.haulmont.bali.db.QueryRunner)

Example 99 with Transaction

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

the class SoftDeleteTest method testUpdateQuery_CleanupMode.

@Test
public void testUpdateQuery_CleanupMode() {
    System.out.println("===================== BEGIN testUpdateQuery_CleanupMode =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        em.setSoftDeletion(false);
        Query query = em.createQuery("update sec$Role r set r.description = ?1 where r.name = ?2");
        query.setParameter(1, "Updated");
        query.setParameter(2, "roleToBeDeleted");
        int updated = query.executeUpdate();
        assertEquals(1, updated);
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testUpdateQuery_CleanupMode =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

Example 100 with Transaction

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

the class SoftDeleteTest method testManyToOne_InnerJoinOnClause.

@Test
public void testManyToOne_InnerJoinOnClause() throws SQLException {
    System.out.println("===================== BEGIN testManyToOne =====================");
    QueryRunner queryRunner = new QueryRunner(persistence.getDataSource());
    queryRunner.update("update SEC_GROUP set DELETE_TS = current_timestamp, DELETED_BY = 'admin' where ID = ?", new Object[] { groupId.toString() });
    // test without view
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        boolean prevValue = setPrintInnerJoinInWhereClause(em, false);
        Group group = null;
        try {
            User user = em.find(User.class, userId);
            group = user.getGroup();
        } finally {
            setPrintInnerJoinInWhereClause(em, prevValue);
        }
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    View view;
    // test fetchMode = AUTO (JOIN is used)
    view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        boolean prevValue = setPrintInnerJoinInWhereClause(em, false);
        Group group;
        try {
            User user = em.find(User.class, userId, view);
            group = user.getGroup();
        } finally {
            setPrintInnerJoinInWhereClause(em, prevValue);
        }
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    // test fetchMode = UNDEFINED
    view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"), FetchMode.UNDEFINED);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        boolean prevValue = setPrintInnerJoinInWhereClause(em, false);
        Group group;
        try {
            User user = em.find(User.class, userId, view);
            group = user.getGroup();
        } finally {
            setPrintInnerJoinInWhereClause(em, prevValue);
        }
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    // test fetchMode = BATCH
    view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"), FetchMode.BATCH);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        boolean prevValue = setPrintInnerJoinInWhereClause(em, false);
        Group group;
        try {
            User user = em.find(User.class, userId, view);
            group = user.getGroup();
        } finally {
            setPrintInnerJoinInWhereClause(em, prevValue);
        }
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    System.out.println("===================== END testManyToOne =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) QueryRunner(com.haulmont.bali.db.QueryRunner)

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