Search in sources :

Example 56 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

the class SoftDeleteTest method testReferenceToDeletedEntityThroughOneToMany.

@Test
public void testReferenceToDeletedEntityThroughOneToMany() throws Exception {
    FetchPlan userRoleView = new View(UserRole.class).addProperty("role", new View(Role.class).addProperty("deleteTs"));
    FetchPlan userView = new View(User.class).addProperty("userRoles", userRoleView);
    Role deleted = persistence.callInTransaction((em) -> em.find(Role.class, role3Id));
    assertNull(deleted);
    UserRole userRole = persistence.callInTransaction((em) -> em.find(UserRole.class, userRole3Id, userRoleView));
    assertNotNull(userRole.getRole());
    assertEquals(role3Id, userRole.getRole().getId());
    assertTrue(userRole.getRole().isDeleted());
    User user = persistence.callInTransaction((em) -> em.find(User.class, user1Id, userView));
    assertEquals(role3Id, user.getUserRoles().iterator().next().getRole().getId());
    Assertions.assertTrue(user.getUserRoles().iterator().next().getRole().isDeleted());
}
Also used : 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 57 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

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 = 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"));
        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 = 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"), 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 = 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"), 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 : 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 58 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

the class SoftDeleteTest method testOneToMany_CleanupMode.

@Test
public void testOneToMany_CleanupMode() {
    System.out.println("===================== BEGIN testOneToMany_CleanupMode =====================");
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        em.setSoftDeletion(false);
        FetchPlan view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("userRoles", new View(UserRole.class, "testView").addProperty("role", new View(Role.class, "testView").addProperty("name")));
        User user = em.find(User.class, userId, view);
        List<UserRole> userRoles = user.getUserRoles();
        assertEquals(2, userRoles.size());
        for (UserRole ur : userRoles) {
            assertNotNull(ur.getRole());
        }
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToMany_CleanupMode =====================");
}
Also used : JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) 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 59 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

the class SoftDeleteTest method testOneToOneMappedByQuery.

@Test
public void testOneToOneMappedByQuery() {
    System.out.println("===================== BEGIN testOneToOneMappedByQuery =====================");
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        SoftDeleteOneToOneA oneToOneA3 = metadata.create(SoftDeleteOneToOneA.class);
        oneToOneA3.setName("oneToOneA3");
        oneToOneA3.setB(em.find(SoftDeleteOneToOneB.class, oneToOneB1Id));
        em.persist(oneToOneA3);
        oneToOneA3Id = oneToOneA3.getId();
        tx.commit();
    } finally {
        tx.end();
    }
    tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        FetchPlan view = new View(SoftDeleteOneToOneB.class, "testView").addProperty("name").addProperty("a", new View(SoftDeleteOneToOneA.class, "testView").addProperty("name"));
        List<SoftDeleteOneToOneB> r = em.createQuery("select b from test$SoftDeleteOneToOneB b where b.name = :name", SoftDeleteOneToOneB.class).setParameter("name", "oneToOneB1").setView(view).getResultList();
        assertEquals(1, r.size());
        assertEquals(r.get(0).getA().getId(), oneToOneA3Id);
        assertEquals(r.get(0).getA().getName(), "oneToOneA3");
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToOneMappedByQuery =====================");
}
Also used : JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) SoftDeleteOneToOneA(com.haulmont.cuba.core.model.SoftDeleteOneToOneA) SoftDeleteOneToOneB(com.haulmont.cuba.core.model.SoftDeleteOneToOneB) 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 60 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

the class NativeQueryTest method testNativeQueryWithDataManager.

@Test
@Disabled
public void testNativeQueryWithDataManager() {
    // https://www.cuba-platform.ru/discuss/t/problema-s-podgruzkoj-obekta-po-view-dobavlennogo-v-toj-zhe-tranzakiczii/5123
    // given:
    Customer customer = dataManager.create(Customer.class);
    customer.setName("c1");
    Order order = dataManager.create(Order.class);
    order.setNumber("c1");
    order.setCustomer(customer);
    dataManager.save(customer, order);
    storeAwareLocator.getTransactionTemplate(Stores.MAIN).executeWithoutResult(tx -> {
        EntityManager entityManager = storeAwareLocator.getEntityManager(Stores.MAIN);
        entityManager.createNativeQuery("select id from SALES_ORDER where ID = ?1", Order.class).setParameter(1, order.getId()).getSingleResult();
        Order reloadedOrder = dataManager.load(Order.class).id(order.getId()).fetchPlan(builder -> {
            builder.add("number").add("customer", FetchPlan.LOCAL);
        }).one();
        assertEquals("c1", reloadedOrder.getNumber());
        assertEquals(customer, reloadedOrder.getCustomer());
    });
}
Also used : Order(test_support.entity.sales.Order) BeforeEach(org.junit.jupiter.api.BeforeEach) DataManager(io.jmix.core.DataManager) DataAccessException(org.springframework.dao.DataAccessException) SpringExtension(org.springframework.test.context.junit.jupiter.SpringExtension) CoreConfiguration(io.jmix.core.CoreConfiguration) FetchPlan(io.jmix.core.FetchPlan) Autowired(org.springframework.beans.factory.annotation.Autowired) Customer(test_support.entity.sales.Customer) Order(test_support.entity.sales.Order) EntityManager(javax.persistence.EntityManager) Disabled(org.junit.jupiter.api.Disabled) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataConfiguration(io.jmix.data.DataConfiguration) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Stores(io.jmix.core.Stores) ContextConfiguration(org.springframework.test.context.ContextConfiguration) TestContextInititalizer(test_support.TestContextInititalizer) EclipselinkConfiguration(io.jmix.eclipselink.EclipselinkConfiguration) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DataTestConfiguration(test_support.DataTestConfiguration) StoreAwareLocator(io.jmix.data.StoreAwareLocator) EntityManager(javax.persistence.EntityManager) Customer(test_support.entity.sales.Customer) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

FetchPlan (io.jmix.core.FetchPlan)61 Test (org.junit.jupiter.api.Test)48 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)45 View (com.haulmont.cuba.core.global.View)35 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)10 Pet (com.haulmont.cuba.core.model.Pet)5 SoftDeleteOneToOneA (com.haulmont.cuba.core.model.SoftDeleteOneToOneA)5 Group (com.haulmont.cuba.core.model.common.Group)5 User (com.haulmont.cuba.core.model.common.User)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 DataManager (com.haulmont.cuba.core.global.DataManager)3 LoadContext (com.haulmont.cuba.core.global.LoadContext)3 FetchPlanProperty (io.jmix.core.FetchPlanProperty)3 Metadata (io.jmix.core.Metadata)3 MetadataTools (io.jmix.core.MetadataTools)3 MetaClass (io.jmix.core.metamodel.model.MetaClass)3 SoftDeleteOneToOneB (com.haulmont.cuba.core.model.SoftDeleteOneToOneB)2 Permission (com.haulmont.cuba.core.model.common.Permission)2 QueryImpl (com.haulmont.cuba.core.sys.QueryImpl)2 Entity (io.jmix.core.Entity)2