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();
}
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());
}
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;
}
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());
}
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());
}
Aggregations