use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.
the class EntityCacheTestClass method testNativeQuery.
@Test
public void testNativeQuery() throws Exception {
appender.clearMessages();
loadUser();
// User, Group
assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
try (Transaction tx = cont.persistence().createTransaction()) {
Query query = cont.entityManager().createNativeQuery("update sec_user set position_ = ? where login_lc = ?");
query.setParameter(1, "new position");
query.setParameter(2, this.user.getLoginLowerCase());
// all evicted here
query.executeUpdate();
tx.commit();
}
appender.clearMessages();
User u;
try (Transaction tx = cont.persistence().createTransaction()) {
u = cont.entityManager().find(User.class, this.user.getId(), "user.browse");
assertNotNull(u);
tx.commit();
}
u = reserialize(u);
Group g = u.getGroup();
assertEquals(this.group, g);
assertNotNull(g.getName());
assertEquals("new position", u.getPosition());
// User,Group
assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count());
}
use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.
the class QueryCacheTestClass method testUpdateQuery.
@Test
public void testUpdateQuery() throws Exception {
appender.clearMessages();
List<User> resultList;
getResultListUserByRole("TestRole");
// User, Group
assertEquals(5, appender.filterMessages(m -> m.contains("> SELECT")).count());
assertEquals(1, queryCache.size());
appender.clearMessages();
try (Transaction tx = cont.persistence().createTransaction()) {
Query query = cont.entityManager().createQuery("update sec$User u set u.position = ?1 where u.loginLowerCase = ?2");
query.setParameter(1, "new position");
query.setParameter(2, this.user.getLoginLowerCase());
query.executeUpdate();
tx.commit();
}
appender.clearMessages();
assertEquals(0, queryCache.size());
resultList = getResultListUserByRole("TestRole");
assertEquals("new position", resultList.get(0).getPosition());
assertEquals(1, queryCache.size());
assertEquals(3, appender.filterMessages(m -> m.contains("> SELECT")).count());
try (Transaction tx = cont.persistence().createTransaction()) {
Query query = cont.entityManager().createQuery("update sec$Role r set r.description = ?1 where r.name = ?2");
query.setParameter(1, "New Role Description");
query.setParameter(2, "TestRole");
query.executeUpdate();
tx.commit();
}
appender.clearMessages();
assertEquals(0, queryCache.size());
getResultListUserByRole("TestRole");
assertEquals(1, queryCache.size());
assertEquals(4, appender.filterMessages(m -> m.contains("> SELECT")).count());
}
use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.
the class QueryCacheTestClass method testWarningInLog.
@Test
@Ignore
public void testWarningInLog() throws Exception {
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
Query query = em.createQuery("select f from sys$Folder f", User.class);
query.setCacheable(true);
query.getResultList();
tx.commit();
}
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
Query query = em.createQuery("select f from sys$Folder f", User.class);
query.setCacheable(true);
query.getResultList();
tx.commit();
}
}
use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.
the class CompositeKeyTest method testListParameter.
@Test
public void testListParameter() throws Exception {
CompositeKeyEntity foo1 = metadata.create(CompositeKeyEntity.class);
EntityKey entityKey1 = metadata.create(EntityKey.class);
entityKey1.setTenant(1);
entityKey1.setEntityId(10L);
foo1.setId(entityKey1);
foo1.setName("foo1");
foo1.setEmail("foo1@mail.com");
CompositeKeyEntity foo2 = metadata.create(CompositeKeyEntity.class);
EntityKey entityKey2 = metadata.create(EntityKey.class);
entityKey2.setTenant(2);
entityKey2.setEntityId(20L);
foo2.setId(entityKey2);
foo2.setName("foo2");
foo2.setEmail("foo2@mail.com");
try (Transaction tx = persistence.createTransaction()) {
persistence.getEntityManager().persist(foo1);
persistence.getEntityManager().persist(foo2);
tx.commit();
}
try (Transaction tx = persistence.createTransaction()) {
Query query = persistence.getEntityManager().createQuery("select e from test$CompositeKeyEntity e " + "where e.id.tenant in :list1 and e.id.entityId in :list2");
query.setParameter("list1", Arrays.asList(entityKey1.getTenant(), entityKey2.getTenant()));
query.setParameter("list2", Arrays.asList(entityKey1.getEntityId(), entityKey2.getEntityId()));
List resultList = query.getResultList();
assertTrue(resultList.contains(foo1) && resultList.contains(foo2));
tx.commit();
}
try (Transaction tx = persistence.createTransaction()) {
persistence.getEntityManager().remove(foo1);
persistence.getEntityManager().remove(foo2);
tx.commit();
}
}
use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.
the class DeletePolicyProcessor method referenceExists.
protected boolean referenceExists(String entityName, MetaProperty property) {
String template = property.getRange().getCardinality().isMany() ? "select count(e) from %s e join e.%s c where c." + primaryKeyName + "= ?1" : "select count(e) from %s e where e.%s." + primaryKeyName + " = ?1";
String qstr = String.format(template, entityName, property.getName());
Query query = entityManager.createQuery(qstr);
query.setParameter(1, entity.getId());
query.setMaxResults(1);
Long count = (Long) query.getSingleResult();
return count > 0;
}
Aggregations