Search in sources :

Example 6 with FetchPlan

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

the class EntityCacheTestClass method testAccessConnectionWithCacheInvalidation.

@Test
@Disabled
public void testAccessConnectionWithCacheInvalidation() {
    appender.clearMessages();
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().getConnection();
        FetchPlanRepository viewRepository = AppBeans.get(FetchPlanRepository.class);
        FetchPlan view = viewRepository.getFetchPlan(metadata.getClass(User.class), "user.browse");
        persistence.getEntityManager().find(User.class, this.user.getId(), view);
        tx.commit();
    }
    // User, Group
    assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().getConnection();
        FetchPlanRepository viewRepository = AppBeans.get(FetchPlanRepository.class);
        FetchPlan view = viewRepository.getFetchPlan(metadata.getClass(User.class), "user.browse");
        persistence.getEntityManager().find(User.class, this.user.getId(), view);
        tx.commit();
    }
    // User, Group
    assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    try (Transaction tx = persistence.createTransaction(new TransactionParams().setReadOnly(true))) {
        try (Transaction tx1 = persistence.getTransaction()) {
            persistence.getEntityManager().getConnection();
            tx1.commit();
        }
        FetchPlanRepository fetchPlanRepository = AppBeans.get(FetchPlanRepository.class);
        FetchPlan fetchPlan = fetchPlanRepository.getFetchPlan(metadata.getClass(User.class), "user.browse");
        Query query = persistence.getEntityManager().createQuery("select u from test$User u where u.id = :id").setParameter("id", user.getId());
        query.setView(View.copy((View) fetchPlan).setLoadPartialEntities(true));
        ((QueryImpl) query).setSingleResultExpected(true);
        User userL = (User) query.getSingleResult();
        // User userL = persistence.getEntityManager().find(User.class, user.getId(), view);
        assertNotNull(userL);
        tx.commit();
    }
    // User, Group
    assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
}
Also used : FetchPlanRepository(io.jmix.core.FetchPlanRepository) QueryImpl(com.haulmont.cuba.core.sys.QueryImpl) FetchPlan(io.jmix.core.FetchPlan)

Example 7 with FetchPlan

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

the class EntityCacheTestClass method loadUserWithRoles.

private User loadUserWithRoles() throws Exception {
    FetchPlan roleView = new View(Role.class).addProperty("name");
    FetchPlan userRoleView = new View(UserRole.class).addProperty("role", roleView);
    FetchPlan groupView = new View(Group.class).addProperty("name");
    FetchPlan userView = new View(User.class).addProperty("login").addProperty("name").addProperty("userRoles", userRoleView).addProperty("group", groupView);
    User user;
    try (Transaction tx = persistence.createTransaction()) {
        user = persistence.getEntityManager().find(User.class, this.user.getId(), userView);
        assertNotNull(user);
        tx.commit();
    }
    user = testSupport.reserialize(user);
    Group g = user.getGroup();
    assertEquals(this.group, g);
    assertNotNull(g.getName());
    assertFail(g::getParent);
    user.getUserRoles().size();
    return user;
}
Also used : FetchPlan(io.jmix.core.FetchPlan) View(com.haulmont.cuba.core.global.View)

Example 8 with FetchPlan

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

the class EntityCacheTestClass method testFindWithView3.

@Test
public void testFindWithView3() throws Exception {
    appender.clearMessages();
    User user;
    // no name in group
    FetchPlan groupView = new View(Group.class, false);
    FetchPlan userView = new View(User.class).addProperty("login").addProperty("group", groupView).setLoadPartialEntities(true);
    try (Transaction tx = persistence.createTransaction()) {
        user = persistence.getEntityManager().find(User.class, this.user.getId(), userView);
        assertNotNull(user);
        tx.commit();
    }
    user = testSupport.reserialize(user);
    Group g = user.getGroup();
    assertNotNull(user.getGroup());
    assertEquals(this.group, g);
    // due to caching, we load all attributes anyway
    assertNotNull(g.getName());
    // user, group
    assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    // second time - from cache
    try (Transaction tx = persistence.createTransaction()) {
        user = persistence.getEntityManager().find(User.class, this.user.getId(), userView);
        assertNotNull(user);
        tx.commit();
    }
    user = testSupport.reserialize(user);
    g = user.getGroup();
    assertEquals(this.group, g);
    assertNotNull(g.getName());
    // user, group
    assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    // name in group - from cache again
    FetchPlan groupView1 = new View(Group.class, true).addProperty("name");
    FetchPlan userView1 = new View(User.class).addProperty("login").addProperty("group", groupView1).setLoadPartialEntities(true);
    try (Transaction tx = persistence.createTransaction()) {
        user = persistence.getEntityManager().find(User.class, this.user.getId(), userView1);
        assertNotNull(user);
        tx.commit();
    }
    user = testSupport.reserialize(user);
    g = user.getGroup();
    assertEquals(this.group, g);
    assertNotNull(g.getName());
    // user, group
    assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
}
Also used : FetchPlan(io.jmix.core.FetchPlan) View(com.haulmont.cuba.core.global.View)

Example 9 with FetchPlan

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

the class QueryCacheTestClass method testLoadingWithNotPartialView.

@Test
public void testLoadingWithNotPartialView() throws Exception {
    appender.clearMessages();
    User user;
    List<User> resultList;
    // load with view: one parameter login
    FetchPlan userLoginView = new View(User.class).addProperty("login");
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
        query.setCacheable(true);
        query.setView(userLoginView);
        resultList = query.getResultList();
        assertEquals(resultList.size(), 2);
        tx.commit();
    }
    user = testSupport.reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    user = testSupport.reserialize(resultList.get(1));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    // user
    assertEquals(1, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
        query.setCacheable(true);
        query.setView(userLoginView);
        resultList = query.getResultList();
        assertEquals(resultList.size(), 2);
        tx.commit();
    }
    user = testSupport.reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    user = testSupport.reserialize(resultList.get(1));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    // user
    assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    // load with view: group
    FetchPlan groupView = new View(Group.class, false).addProperty("name");
    FetchPlan userGroupView = new View(User.class).addProperty("group", groupView);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
        query.setCacheable(true);
        query.setView(userGroupView);
        resultList = query.getResultList();
        assertEquals(resultList.size(), 2);
        tx.commit();
    }
    user = testSupport.reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertEquals(user.getGroup(), group);
    assertEquals(user.getGroup().getName(), group.getName());
    user = testSupport.reserialize(resultList.get(1));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertEquals(user.getGroup(), group);
    assertEquals(user.getGroup().getName(), group.getName());
    assertEquals(1, appender.filterMessages(m -> m.contains("> SELECT") && m.contains("TEST_GROUP")).count());
    appender.clearMessages();
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
        query.setCacheable(true);
        query.setView(userGroupView);
        resultList = query.getResultList();
        assertEquals(resultList.size(), 2);
        tx.commit();
    }
    user = testSupport.reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertEquals(user.getGroup(), group);
    assertEquals(user.getGroup().getName(), group.getName());
    user = testSupport.reserialize(resultList.get(1));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertEquals(user.getGroup(), group);
    assertEquals(user.getGroup().getName(), group.getName());
    assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
        query.setCacheable(true);
        query.setView(userLoginView);
        resultList = query.getResultList();
        assertEquals(resultList.size(), 2);
        tx.commit();
    }
    user = testSupport.reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertFail(user::getGroup);
    user = testSupport.reserialize(resultList.get(1));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertFail(user::getGroup);
    assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<User> query = em.createQuery("select u from test$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
        query.setCacheable(true);
        query.setView(userGroupView);
        resultList = query.getResultList();
        assertEquals(resultList.size(), 2);
        tx.commit();
    }
    user = testSupport.reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertEquals(user.getGroup(), group);
    assertEquals(user.getGroup().getName(), group.getName());
    user = testSupport.reserialize(resultList.get(1));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertEquals(user.getGroup(), group);
    assertEquals(user.getGroup().getName(), group.getName());
    assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
}
Also used : FetchPlan(io.jmix.core.FetchPlan) Test(org.junit.jupiter.api.Test)

Example 10 with FetchPlan

use of io.jmix.core.FetchPlan 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)

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