Search in sources :

Example 51 with EntityManager

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

the class SoftDeleteTest method testOneToManyLazy.

@Test
public void testOneToManyLazy() {
    System.out.println("===================== BEGIN testOneToManyLazy =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, userId);
        List<UserRole> userRoles = user.getUserRoles();
        assertEquals(1, userRoles.size());
        for (UserRole ur : userRoles) {
            assertNotNull(ur.getRole());
        }
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToManyLazy =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 52 with EntityManager

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

the class SoftDeleteTest method setUp.

@Before
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);
        Role role1 = em.find(Role.class, UUID.fromString("0c018061-b26f-4de2-a5be-dff348347f93"));
        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);
        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);
        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();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) 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 53 with EntityManager

use of com.haulmont.cuba.core.EntityManager 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) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

Example 54 with EntityManager

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

the class SoftDeleteTest method testCleanupMode.

@Test
public void testCleanupMode() {
    System.out.println("===================== BEGIN testCleanupMode =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        em.setSoftDeletion(false);
        Role role = em.find(Role.class, role2Id);
        assertNotNull(role);
        assertTrue(role.isDeleted());
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testCleanupMode =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 55 with EntityManager

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

the class SoftDeleteTest method testManyToOne.

@Test
public void testManyToOne() 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();
        User user = em.find(User.class, userId);
        Group group = user.getGroup();
        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();
        User user = em.find(User.class, userId, view);
        Group group = user.getGroup();
        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();
        User user = em.find(User.class, userId, view);
        Group group = user.getGroup();
        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();
        User user = em.find(User.class, userId, view);
        Group group = user.getGroup();
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    System.out.println("===================== END testManyToOne =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) QueryRunner(com.haulmont.bali.db.QueryRunner)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)167 Transaction (com.haulmont.cuba.core.Transaction)140 Query (com.haulmont.cuba.core.Query)27 User (com.haulmont.cuba.security.entity.User)27 Test (org.junit.Test)25 View (com.haulmont.cuba.core.global.View)22 MetaClass (com.haulmont.chile.core.model.MetaClass)14 Group (com.haulmont.cuba.security.entity.Group)12 Before (org.junit.Before)11 Entity (com.haulmont.cuba.core.entity.Entity)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 UUID (java.util.UUID)7 Nullable (javax.annotation.Nullable)7 TypedQuery (com.haulmont.cuba.core.TypedQuery)6 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 Role (com.haulmont.cuba.security.entity.Role)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4