Search in sources :

Example 71 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class QueryCacheTestClass method testQueryPaging.

@Test
public void testQueryPaging() throws Exception {
    appender.clearMessages();
    assertEquals(0, queryCache.size());
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
        query.setCacheable(true);
        query.setViewName("user.browse");
        query.setFirstResult(0);
        query.setMaxResults(1);
        List<User> result = query.getResultList();
        assertEquals(result.size(), 1);
        assertUserBrowseView(result.get(0));
        assertEquals(this.user.getLogin(), result.get(0).getLogin());
        assertEquals(this.group, result.get(0).getGroup());
        tx.commit();
    }
    // User, Group
    assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    assertEquals(1, queryCache.size());
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
        query.setCacheable(true);
        query.setViewName("user.browse");
        query.setFirstResult(0);
        query.setMaxResults(1);
        List<User> result = query.getResultList();
        assertEquals(result.size(), 1);
        assertUserBrowseView(result.get(0));
        assertEquals(this.user.getLogin(), result.get(0).getLogin());
        assertEquals(this.group, result.get(0).getGroup());
        tx.commit();
    }
    // User, Group
    assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
    appender.clearMessages();
    assertEquals(1, queryCache.size());
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
        query.setCacheable(true);
        query.setViewName("user.browse");
        query.setFirstResult(1);
        query.setMaxResults(1);
        List<User> result = query.getResultList();
        assertEquals(result.size(), 1);
        assertUserBrowseView(result.get(0));
        assertEquals(this.user2.getLogin(), result.get(0).getLogin());
        assertEquals(this.group, result.get(0).getGroup());
        tx.commit();
    }
    // User, Group
    assertEquals(1, appender.filterMessages(m -> m.contains("> SELECT")).count());
    assertEquals(2, queryCache.size());
    appender.clearMessages();
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$User u where u.login like 'ECTest-%' order by u.name asc", User.class);
        query.setCacheable(true);
        query.setViewName("user.browse");
        query.setFirstResult(1);
        query.setMaxResults(1);
        List<User> result = query.getResultList();
        assertEquals(result.size(), 1);
        assertUserBrowseView(result.get(0));
        assertEquals(this.user2.getLogin(), result.get(0).getLogin());
        assertEquals(this.group, result.get(0).getGroup());
        tx.commit();
    }
    // User, Group
    assertEquals(0, appender.filterMessages(m -> m.contains("> SELECT")).count());
    assertEquals(2, queryCache.size());
    appender.clearMessages();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 72 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class QueryCacheTestClass method testLoadingWithPartialView.

@Test
public void testLoadingWithPartialView() throws Exception {
    appender.clearMessages();
    User user;
    List<User> resultList;
    // load with view: one parameter login
    View userLoginView = new View(User.class).addProperty("login").setLoadPartialEntities(true);
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$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 = reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    user = 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 = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$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 = reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    user = 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
    View groupView = new View(Group.class, false).addProperty("name").setLoadPartialEntities(true);
    View userGroupView = new View(User.class).addProperty("group", groupView).setLoadPartialEntities(true);
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$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 = reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertEquals(user.getGroup(), group);
    assertEquals(user.getGroup().getName(), group.getName());
    user = 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("SEC_GROUP")).count());
    appender.clearMessages();
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$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 = reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertEquals(user.getGroup(), group);
    assertEquals(user.getGroup().getName(), group.getName());
    user = 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 = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$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 = reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertFail(user::getGroup);
    user = 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 = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$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 = reserialize(resultList.get(0));
    assertNotNull(user.getLogin());
    assertNotNull(user.getName());
    assertEquals(user.getGroup(), group);
    assertEquals(user.getGroup().getName(), group.getName());
    user = 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 : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View)

Example 73 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class QueryCacheTestClass method getResultListUserByLoginNamed.

protected User getResultListUserByLoginNamed(User loadedUser, boolean checkView, Consumer<EntityManager> emBuilder, Consumer<Query> queryBuilder) throws Exception {
    User user;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        if (emBuilder != null) {
            emBuilder.accept(em);
        }
        TypedQuery<User> query = em.createQuery("select u from sec$User u where u.login = :login", User.class);
        query.setCacheable(true);
        query.setParameter("login", "ECTest-" + loadedUser.getId());
        query.setViewName("user.browse");
        if (queryBuilder != null) {
            queryBuilder.accept(query);
        }
        List<User> resultList = query.getResultList();
        user = resultList.isEmpty() ? null : resultList.get(0);
        tx.commit();
    }
    if (user != null && checkView) {
        assertUserBrowseView(user);
    }
    return user;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 74 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class QueryCacheTestClass method testNotEntityQuery.

@Test
public void testNotEntityQuery() throws Exception {
    appender.clearMessages();
    assertEquals(0, queryCache.size());
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        Query query = em.createQuery("select u, u.name from sec$User u where u.login = :login");
        query.setCacheable(true);
        query.setParameter("login", "ECTest-" + user.getId());
        query.getResultList();
        tx.commit();
    }
    assertEquals(0, queryCache.size());
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        Query query = em.createQuery("select u.name from sec$User u where u.login = :login");
        query.setCacheable(true);
        query.setParameter("login", "ECTest-" + user.getId());
        query.getResultList();
        tx.commit();
    }
    assertEquals(0, queryCache.size());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) TypedQuery(com.haulmont.cuba.core.TypedQuery)

Example 75 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class QueryCacheTestClass method testStaleData_UpdateParentEntity.

@Test
public void testStaleData_UpdateParentEntity() throws Exception {
    appender.clearMessages();
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        Query query = em.createQuery("select f from sys$Folder f");
        query.setCacheable(true);
        query.getResultList();
        tx.commit();
    }
    assertEquals(1, queryCache.size());
    appender.clearMessages();
    appFolder = cont.metadata().create(AppFolder.class);
    appFolder.setName("new app folder");
    try (Transaction tx = cont.persistence().createTransaction()) {
        cont.entityManager().persist(appFolder);
        tx.commit();
    }
    appender.clearMessages();
    assertEquals(0, queryCache.size());
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        Query query = em.createQuery("select f from sys$Folder f");
        query.setCacheable(true);
        query.getResultList();
        tx.commit();
    }
    assertEquals(1, queryCache.size());
}
Also used : AppFolder(com.haulmont.cuba.core.entity.AppFolder) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) TypedQuery(com.haulmont.cuba.core.TypedQuery)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)167 Transaction (com.haulmont.cuba.core.Transaction)140 Query (com.haulmont.cuba.core.Query)27 User (com.haulmont.cuba.security.entity.User)27 Test (org.junit.Test)25 View (com.haulmont.cuba.core.global.View)22 MetaClass (com.haulmont.chile.core.model.MetaClass)14 Group (com.haulmont.cuba.security.entity.Group)12 Before (org.junit.Before)11 Entity (com.haulmont.cuba.core.entity.Entity)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 UUID (java.util.UUID)7 Nullable (javax.annotation.Nullable)7 TypedQuery (com.haulmont.cuba.core.TypedQuery)6 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 Role (com.haulmont.cuba.security.entity.Role)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4