Search in sources :

Example 41 with FetchPlan

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

the class FetchJoinTest method testNotLoadingCustomer.

@Test
public void testNotLoadingCustomer() throws Exception {
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        FetchPlan partyView = new View(Party.class).addProperty("name");
        FetchPlan productView = new View(Product.class).addProperty("name");
        FetchPlan customerView = new View(Customer.class).addProperty("customerNumber").addProperty("party", partyView);
        FetchPlan salesPersonView = new View(SalesPerson.class).addProperty("salespersonNumber").addProperty("party", partyView);
        FetchPlan orderView = new View(Order.class).addProperty("orderNumber").addProperty("customer", customerView).addProperty("salesPerson", salesPersonView);
        FetchPlan orderLineView = new View(OrderLine.class).addProperty("order", orderView).addProperty("product", productView);
        OrderLine reloadedOrderLine = em.find(OrderLine.class, orderLine.getId(), orderLineView);
        assertNotNull(reloadedOrderLine);
        assertNotNull(reloadedOrderLine.getOrder().getCustomer());
        assertEquals(partyCustomer, reloadedOrderLine.getOrder().getCustomer().getParty());
        assertNotNull(reloadedOrderLine.getOrder().getSalesPerson());
        assertEquals(partyPerson, reloadedOrderLine.getOrder().getSalesPerson().getParty());
        tx.commit();
    }
}
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 42 with FetchPlan

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

the class DataManagerCommit2Test method testViewOnSecondLevelAfterCommitNew.

@Test
public void testViewOnSecondLevelAfterCommitNew() throws Exception {
    FetchPlan groupView = new View(Group.class, false).addProperty("createTs").addProperty("constraints", new View(Constraint.class, false).addProperty("createTs")).setLoadPartialEntities(true);
    Group group = dataManager.load(LoadContext.create(Group.class).setId(this.group2.getId()).setView(groupView));
    assertNotNull(group);
    assertFalse(entityStates.isLoaded(group.getConstraints().iterator().next(), "entityName"));
    User user = metadata.create(User.class);
    try {
        user.setName("testUser");
        user.setLogin("login" + user.getId());
        user.setGroup(group);
        FetchPlan userView = new View(User.class, true).addProperty("login").addProperty("name").addProperty("group", new View(Group.class, false).addProperty("createTs").addProperty("constraints", new View(Constraint.class, false).addProperty("entityName")));
        User committedUser = dataManager.commit(user, userView);
        assertTrue(entityStates.isLoaded(committedUser.getGroup(), "createTs"));
        assertTrue(entityStates.isLoaded(committedUser.getGroup().getConstraints().iterator().next(), "entityName"));
    } finally {
        testSupport.deleteRecord(user);
    }
}
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 43 with FetchPlan

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

the class EclipseLinkDetachedTest method testSerializedFetchGroup.

@Test
public void testSerializedFetchGroup() throws Exception {
    Transaction tx;
    EntityManager em;
    User user;
    tx = persistence.createTransaction();
    try {
        em = persistence.getEntityManager();
        FetchPlan view = new View(User.class).addProperty("login").setLoadPartialEntities(true);
        user = em.find(User.class, userId, view);
        assertNotNull(user);
        tx.commit();
    } finally {
        tx.end();
    }
    user = testSupport.reserialize(user);
    assertEquals("testLogin", user.getLogin());
    // exception on getting not loaded references
    try {
        user.getName();
        fail();
    } catch (Exception ignored) {
    }
    try {
        assertFalse(entityStates.isLoaded(user, "group"));
        user.getGroup();
    } catch (Exception ignored) {
    }
    try {
        assertFalse(entityStates.isLoaded(user, "userRoles"));
        user.getUserRoles().size();
    } catch (Exception ignored) {
    }
}
Also used : User(com.haulmont.cuba.core.model.common.User) 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 44 with FetchPlan

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

the class EclipseLinkQueriesTest method testJoinOnWithToManyView.

// join on, view contains ToMany attribute
@Test
public void testJoinOnWithToManyView() throws Exception {
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        FetchPlan view = new View(Group.class).addProperty("constraints");
        TypedQuery<Group> query = em.createQuery("select g from test$Group g join test$QueryResult qr on qr.entityId = g.id where qr.queryKey = 1", Group.class);
        query.setView(view);
        List<Group> result = query.getResultList();
        tx.commit();
    }
}
Also used : Group(com.haulmont.cuba.core.model.common.Group) 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 45 with FetchPlan

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

the class EclipseLinkQueriesTest method testCrossJoinViewParentReference.

// cross join, view with the reference to the parent entity
@Test
public void testCrossJoinViewParentReference() throws Exception {
    List<Group> result;
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        FetchPlan view = new View(Group.class).addProperty("parent");
        TypedQuery<Group> query = em.createQuery("select g from test$Group g, test$User u where u.group = g", Group.class);
        query.setView(view);
        result = query.getResultList();
        tx.commit();
    }
    for (Group g : result) {
        g = testSupport.reserialize(g);
        if (g.equals(rootGroup))
            assertNull(g.getParent());
        else if (g.equals(group))
            assertEquals(rootGroup, g.getParent());
    }
}
Also used : Group(com.haulmont.cuba.core.model.common.Group) 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)

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