Search in sources :

Example 1 with SoftDeleteOneToOneA

use of com.haulmont.cuba.core.model.SoftDeleteOneToOneA in project jmix by jmix-framework.

the class SoftDeleteTest method testOneToOneQuery.

@Test
public void testOneToOneQuery() {
    System.out.println("===================== BEGIN testOneToOneQuery =====================");
    // test fetchMode = AUTO
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        FetchPlan view = new View(SoftDeleteOneToOneA.class, "testView").addProperty("name").addProperty("b", new View(SoftDeleteOneToOneB.class, "testView").addProperty("name"));
        List<SoftDeleteOneToOneA> r = em.createQuery("select a from test$SoftDeleteOneToOneA a where a.name = :name", SoftDeleteOneToOneA.class).setParameter("name", "oneToOneA2").setView(view).getResultList();
        assertEquals(1, r.size());
        assertEquals(r.get(0).getB().getId(), oneToOneB2Id);
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToOneQuery =====================");
}
Also used : JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) SoftDeleteOneToOneA(com.haulmont.cuba.core.model.SoftDeleteOneToOneA) FetchPlan(io.jmix.core.FetchPlan) View(com.haulmont.cuba.core.global.View) CoreTest(com.haulmont.cuba.core.testsupport.CoreTest) Test(org.junit.jupiter.api.Test)

Example 2 with SoftDeleteOneToOneA

use of com.haulmont.cuba.core.model.SoftDeleteOneToOneA in project jmix by jmix-framework.

the class SoftDeleteTest method testOneToOneQuery_InnerJoinOnClause.

@Test
public void testOneToOneQuery_InnerJoinOnClause() {
    System.out.println("===================== BEGIN testOneToOneQuery_InnerJoinOnClause =====================");
    // test fetchMode = AUTO
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        boolean prevValue = setPrintInnerJoinInWhereClause(em, false);
        try {
            FetchPlan view = new View(SoftDeleteOneToOneA.class, "testView").addProperty("name").addProperty("b", new View(SoftDeleteOneToOneB.class, "testView").addProperty("name"));
            List<SoftDeleteOneToOneA> r = em.createQuery("select a from test$SoftDeleteOneToOneA a where a.name = :name", SoftDeleteOneToOneA.class).setParameter("name", "oneToOneA2").setView(view).getResultList();
            assertEquals(1, r.size());
            assertEquals(r.get(0).getB().getId(), oneToOneB2Id);
        } finally {
            setPrintInnerJoinInWhereClause(em, prevValue);
        }
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToOneQuery_InnerJoinOnClause =====================");
}
Also used : JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) SoftDeleteOneToOneA(com.haulmont.cuba.core.model.SoftDeleteOneToOneA) FetchPlan(io.jmix.core.FetchPlan) View(com.haulmont.cuba.core.global.View) CoreTest(com.haulmont.cuba.core.testsupport.CoreTest) Test(org.junit.jupiter.api.Test)

Example 3 with SoftDeleteOneToOneA

use of com.haulmont.cuba.core.model.SoftDeleteOneToOneA in project jmix by jmix-framework.

the class SoftDeleteTest method testOneToOneLazy.

@Test
public void testOneToOneLazy() {
    System.out.println("===================== BEGIN testOneToOneLazy =====================");
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA2Id);
        assertNotNull(oneToOneA);
        assertNotNull(oneToOneA.getB());
        assertEquals(oneToOneA.getB().getId(), oneToOneB2Id);
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToOneLazy =====================");
}
Also used : JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) SoftDeleteOneToOneA(com.haulmont.cuba.core.model.SoftDeleteOneToOneA) CoreTest(com.haulmont.cuba.core.testsupport.CoreTest) Test(org.junit.jupiter.api.Test)

Example 4 with SoftDeleteOneToOneA

use of com.haulmont.cuba.core.model.SoftDeleteOneToOneA in project jmix by jmix-framework.

the class SoftDeleteTest method testOneToOne_InnerJoinOnClause.

@Test
public void testOneToOne_InnerJoinOnClause() {
    System.out.println("===================== BEGIN testOneToOne_InnerJoinOnClause =====================");
    // test fetchMode = AUTO
    System.out.println("===================== BEGIN testOneToOne_InnerJoinOnClause fetchMode = AUTO =====================");
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        boolean prevValue = setPrintInnerJoinInWhereClause(em, false);
        try {
            FetchPlan 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);
        } finally {
            setPrintInnerJoinInWhereClause(em, prevValue);
        }
        tx.commit();
    } finally {
        tx.end();
    }
    // test fetchMode = BATCH
    System.out.println("===================== BEGIN testOneToOne_InnerJoinOnClause fetchMode = BATCH =====================");
    tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        boolean prevValue = setPrintInnerJoinInWhereClause(em, false);
        try {
            FetchPlan 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);
        } finally {
            setPrintInnerJoinInWhereClause(em, prevValue);
        }
        tx.commit();
    } finally {
        tx.end();
    }
    // test fetchMode = UNDEFINED
    System.out.println("===================== BEGIN testOneToOne_InnerJoinOnClause fetchMode = UNDEFINED =====================");
    tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        boolean prevValue = setPrintInnerJoinInWhereClause(em, false);
        try {
            FetchPlan 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);
        } finally {
            setPrintInnerJoinInWhereClause(em, prevValue);
        }
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToOne =====================");
}
Also used : JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) SoftDeleteOneToOneA(com.haulmont.cuba.core.model.SoftDeleteOneToOneA) FetchPlan(io.jmix.core.FetchPlan) View(com.haulmont.cuba.core.global.View) CoreTest(com.haulmont.cuba.core.testsupport.CoreTest) Test(org.junit.jupiter.api.Test)

Example 5 with SoftDeleteOneToOneA

use of com.haulmont.cuba.core.model.SoftDeleteOneToOneA in project jmix by jmix-framework.

the class SoftDeleteTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = 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 = metadata.create(SoftDeleteOneToOneB.class);
        oneToOneB1.setName("oneToOneB1");
        em.persist(oneToOneB1);
        oneToOneB1Id = oneToOneB1.getId();
        SoftDeleteOneToOneB oneToOneB2 = metadata.create(SoftDeleteOneToOneB.class);
        oneToOneB2.setName("oneToOneB2");
        em.persist(oneToOneB2);
        oneToOneB2Id = oneToOneB2.getId();
        SoftDeleteOneToOneA oneToOneA1 = metadata.create(SoftDeleteOneToOneA.class);
        oneToOneA1.setName("oneToOneA1");
        oneToOneA1.setB(oneToOneB1);
        em.persist(oneToOneA1);
        oneToOneA1Id = oneToOneA1.getId();
        SoftDeleteOneToOneA oneToOneA2 = 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("test$Constraint");
        constraint1.setGroup(group1);
        em.persist(constraint1);
        Constraint constraint2 = new Constraint();
        constraint2Id = constraint2.getId();
        constraint2.setCode("constraint2");
        constraint2.setEntityName("test$Constraint");
        constraint2.setGroup(group1);
        em.persist(constraint2);
        tx.commitRetaining();
        em = 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
        JdbcTemplate jdbcTemplate = new JdbcTemplate(persistence.getDataSource());
        jdbcTemplate.update("update TEST_ROLE set DELETE_TS = CREATE_TS, DELETED_BY = CREATED_BY where name = 'roleToBeDeleted3'");
        tx.commit();
    } finally {
        tx.end();
    }
    JdbcTemplate jdbcTemplate = new JdbcTemplate(persistence.getDataSource());
    jdbcTemplate.update("update TEST_USER set DELETE_TS = current_timestamp, DELETED_BY = 'admin' where ID = ?", user2Id.toString());
}
Also used : JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) SoftDeleteOneToOneA(com.haulmont.cuba.core.model.SoftDeleteOneToOneA) SoftDeleteOneToOneB(com.haulmont.cuba.core.model.SoftDeleteOneToOneB) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

SoftDeleteOneToOneA (com.haulmont.cuba.core.model.SoftDeleteOneToOneA)7 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)7 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)6 Test (org.junit.jupiter.api.Test)6 View (com.haulmont.cuba.core.global.View)5 FetchPlan (io.jmix.core.FetchPlan)5 SoftDeleteOneToOneB (com.haulmont.cuba.core.model.SoftDeleteOneToOneB)2 BeforeEach (org.junit.jupiter.api.BeforeEach)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1