Search in sources :

Example 1 with FetchPlanRepository

use of io.jmix.core.FetchPlanRepository 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 2 with FetchPlanRepository

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

Aggregations

QueryImpl (com.haulmont.cuba.core.sys.QueryImpl)2 FetchPlan (io.jmix.core.FetchPlan)2 FetchPlanRepository (io.jmix.core.FetchPlanRepository)2 Logger (ch.qos.logback.classic.Logger)1 LoggerContext (ch.qos.logback.classic.LoggerContext)1 com.haulmont.cuba.core (com.haulmont.cuba.core)1 AppBeans (com.haulmont.cuba.core.global.AppBeans)1 DataManager (com.haulmont.cuba.core.global.DataManager)1 LoadContext (com.haulmont.cuba.core.global.LoadContext)1 View (com.haulmont.cuba.core.global.View)1 com.haulmont.cuba.core.model.common (com.haulmont.cuba.core.model.common)1 ChildCachedEntity (com.haulmont.cuba.core.model.entity_cache.ChildCachedEntity)1 ParentCachedEntity (com.haulmont.cuba.core.model.entity_cache.ParentCachedEntity)1 CompositeOne (com.haulmont.cuba.core.model.entitycache_unfetched.CompositeOne)1 CompositePropertyOne (com.haulmont.cuba.core.model.entitycache_unfetched.CompositePropertyOne)1 CompositePropertyTwo (com.haulmont.cuba.core.model.entitycache_unfetched.CompositePropertyTwo)1 CompositeTwo (com.haulmont.cuba.core.model.entitycache_unfetched.CompositeTwo)1 com.haulmont.cuba.core.testsupport (com.haulmont.cuba.core.testsupport)1 TestAssertions.assertFail (com.haulmont.cuba.core.testsupport.TestAssertions.assertFail)1 Metadata (io.jmix.core.Metadata)1