Search in sources :

Example 46 with FetchPlan

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

the class EntityCacheTestClass method testLoadingRelatedEntityFromCache.

@Test
public void testLoadingRelatedEntityFromCache() {
    appender.clearMessages();
    FetchPlanRepository viewRepository = AppBeans.get(FetchPlanRepository.class);
    FetchPlan view = viewRepository.getFetchPlan(metadata.getClass(UserSubstitution.class), "usersubst.edit");
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().find(UserSubstitution.class, this.userSubstitution.getId(), view);
        tx.commit();
    }
    // UserSubstitution, User, User
    assertEquals(3, appender.filterMessages(selectsOnly).count());
    // User must not be joined because it is cached
    assertTrue(appender.filterMessages(selectsOnly).noneMatch(s -> s.contains("JOIN TEST_USER")));
    appender.clearMessages();
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().find(UserSubstitution.class, this.userSubstitution.getId(), view);
        tx.commit();
    }
    // UserSubstitution only, User is cached
    assertEquals(1, appender.filterMessages(selectsOnly).count());
    // User must not be joined because it is cached
    assertTrue(appender.filterMessages(selectsOnly).noneMatch(s -> s.contains("JOIN TEST_USER")));
}
Also used : LoadContext(com.haulmont.cuba.core.global.LoadContext) DirtiesContext(org.springframework.test.annotation.DirtiesContext) com.haulmont.cuba.core(com.haulmont.cuba.core) com.haulmont.cuba.core.testsupport(com.haulmont.cuba.core.testsupport) TestRule(org.junit.rules.TestRule) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) LoggerContext(ch.qos.logback.classic.LoggerContext) AppBeans(com.haulmont.cuba.core.global.AppBeans) FetchPlanRepository(io.jmix.core.FetchPlanRepository) TestAssertions.assertFail(com.haulmont.cuba.core.testsupport.TestAssertions.assertFail) CompositeOne(com.haulmont.cuba.core.model.entitycache_unfetched.CompositeOne) ParentCachedEntity(com.haulmont.cuba.core.model.entity_cache.ParentCachedEntity) CompositePropertyTwo(com.haulmont.cuba.core.model.entitycache_unfetched.CompositePropertyTwo) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) com.haulmont.cuba.core.model.common(com.haulmont.cuba.core.model.common) EntityManagerFactoryDelegate(org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate) MetadataTools(io.jmix.core.MetadataTools) DataManager(com.haulmont.cuba.core.global.DataManager) QueryImpl(com.haulmont.cuba.core.sys.QueryImpl) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) Predicate(java.util.function.Predicate) FetchPlan(io.jmix.core.FetchPlan) Metadata(io.jmix.core.Metadata) TestPropertySource(org.springframework.test.context.TestPropertySource) CompositeTwo(com.haulmont.cuba.core.model.entitycache_unfetched.CompositeTwo) QueryException(org.eclipse.persistence.exceptions.QueryException) View(com.haulmont.cuba.core.global.View) List(java.util.List) CompositePropertyOne(com.haulmont.cuba.core.model.entitycache_unfetched.CompositePropertyOne) Logger(ch.qos.logback.classic.Logger) org.junit.jupiter.api(org.junit.jupiter.api) ChildCachedEntity(com.haulmont.cuba.core.model.entity_cache.ChildCachedEntity) Environment(org.springframework.core.env.Environment) EntityManagerFactory(javax.persistence.EntityManagerFactory) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) JpaCache(org.eclipse.persistence.jpa.JpaCache) Assert(org.junit.Assert) FetchPlanRepository(io.jmix.core.FetchPlanRepository) FetchPlan(io.jmix.core.FetchPlan)

Example 47 with FetchPlan

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

the class EntityCacheTestClass method loadUserSetting.

private UserSetting loadUserSetting() throws Exception {
    UserSetting us;
    FetchPlan usView = new View(UserSetting.class).addProperty("name").addProperty("user", new View(User.class).addProperty("login"));
    try (Transaction tx = persistence.createTransaction()) {
        us = persistence.getEntityManager().find(UserSetting.class, this.userSetting.getId(), usView);
        assertNotNull(us);
        tx.commit();
    }
    us = testSupport.reserialize(us);
    assertEquals(userSetting, us);
    assertEquals(user, us.getUser());
    return us;
}
Also used : FetchPlan(io.jmix.core.FetchPlan) View(com.haulmont.cuba.core.global.View)

Example 48 with FetchPlan

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

the class QueryCacheTestClass method testLoadingWithPartialView.

@Test
public void testLoadingWithPartialView() throws Exception {
    appender.clearMessages();
    User user;
    List<User> resultList;
    // load with view: one parameter login
    FetchPlan userLoginView = new View(User.class).addProperty("login").setLoadPartialEntities(true);
    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").setLoadPartialEntities(true);
    FetchPlan userGroupView = new View(User.class).addProperty("group", groupView).setLoadPartialEntities(true);
    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 49 with FetchPlan

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

the class SoftDeleteNotFoundDeletedTest method testLoadDeletedObject.

@Test
public void testLoadDeletedObject() {
    FetchPlan taskView_Message = new View(SoftDelete_Task.class).addProperty("message");
    FetchPlan taskView_Service = new View(SoftDelete_Task.class).addProperty("service", new View(SoftDelete_Service.class).addProperty("code"));
    FetchPlan taskValueView = new View(SoftDelete_TaskValue.class).addProperty("task", taskView_Message);
    FetchPlan projectView = new View(SoftDelete_Project.class).addProperty("name").addProperty("aValue", taskValueView).addProperty("task", taskView_Service);
    LoadContext<SoftDelete_Project> loadContext = new LoadContext<>(SoftDelete_Project.class).setView(projectView).setId(projectId);
    SoftDelete_Project project = dataManager.load(loadContext);
    Assertions.assertNotNull(project);
    Assertions.assertNotNull(project.getTask());
    Assertions.assertTrue(project.getTask().isDeleted());
}
Also used : SoftDelete_Task(com.haulmont.cuba.core.model.SoftDelete_Task) SoftDelete_TaskValue(com.haulmont.cuba.core.model.SoftDelete_TaskValue) LoadContext(com.haulmont.cuba.core.global.LoadContext) SoftDelete_Project(com.haulmont.cuba.core.model.SoftDelete_Project) 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 50 with FetchPlan

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

the class ViewBuilderTest method testLocal.

@Test
public void testLocal() {
    FetchPlan petView = ViewBuilder.of(Pet.class).addView(FetchPlan.LOCAL).build();
    assertTrue(containsSystemProperties(petView));
    assertTrue(petView.containsProperty("name"));
    FetchPlan ownerView = ViewBuilder.of(Owner.class).addView(FetchPlan.LOCAL).build();
    assertTrue(containsSystemProperties(ownerView));
    assertTrue(ownerView.containsProperty("name"));
    assertFalse(ownerView.containsProperty("address"));
}
Also used : FetchPlan(io.jmix.core.FetchPlan) Test(org.junit.jupiter.api.Test) CoreTest(com.haulmont.cuba.core.testsupport.CoreTest)

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