use of com.haulmont.cuba.core.TypedQuery in project cuba by cuba-platform.
the class TestBeforeCommitTxListener method queryWithFlush.
private void queryWithFlush(Collection<Entity> managedEntities, EntityManager entityManager) {
if (!managedEntities.stream().anyMatch(e -> e instanceof User && ((User) e).getLogin().startsWith("TxLstnrTst-")))
return;
TypedQuery<User> query = entityManager.createQuery("select u from sec$User u where u.login like ?1", User.class);
query.setParameter(1, "TxLstnrTst-2-%");
query.setFlushMode(FlushModeType.AUTO);
User result = query.getFirstResult();
assertNotNull(result);
}
use of com.haulmont.cuba.core.TypedQuery in project cuba by cuba-platform.
the class EntityCacheTestClass method testQuery.
@Test
public void testQuery() throws Exception {
appender.clearMessages();
User u1, u2;
List<User> list;
try (Transaction tx = cont.persistence().createTransaction()) {
TypedQuery<User> query = cont.entityManager().createQuery("select u from sec$User u where u.login like ?1", User.class);
query.setParameter(1, "ECTest%");
query.setViewName("user.browse");
list = query.getResultList();
tx.commit();
}
u1 = list.stream().filter(u -> u.getId().equals(this.user.getId())).findFirst().get();
assertEquals(this.user.getLogin(), u1.getLogin());
assertEquals(this.group, u1.getGroup());
u2 = list.stream().filter(user -> user.getId().equals(this.user2.getId())).findFirst().get();
assertEquals(this.user2.getLogin(), u2.getLogin());
assertEquals(this.group, u2.getGroup());
// User, Group
assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count());
appender.clearMessages();
try (Transaction tx = cont.persistence().createTransaction()) {
TypedQuery<User> query = cont.entityManager().createQuery("select u from sec$User u where u.login like ?1", User.class);
query.setParameter(1, "ECTest%");
query.setViewName("user.browse");
list = query.getResultList();
tx.commit();
}
u1 = list.stream().filter(u -> u.getId().equals(this.user.getId())).findFirst().get();
assertEquals(this.user.getLogin(), u1.getLogin());
assertEquals(this.group, u1.getGroup());
u2 = list.stream().filter(user -> user.getId().equals(this.user2.getId())).findFirst().get();
assertEquals(this.user2.getLogin(), u2.getLogin());
assertEquals(this.group, u2.getGroup());
// User only
assertEquals(1, appender.filterMessages(m -> m.contains("> SELECT")).count());
}
use of com.haulmont.cuba.core.TypedQuery in project cuba by cuba-platform.
the class DynamicAttributesManager method loadAttributeValues.
protected List<CategoryAttributeValue> loadAttributeValues(MetaClass metaClass, List<Object> entityIds) {
List<CategoryAttributeValue> attributeValues = new ArrayList<>();
try (Transaction tx = persistence.getTransaction()) {
EntityManager em = persistence.getEntityManager();
View view = new View(viewRepository.getView(CategoryAttributeValue.class, View.LOCAL), null, false).addProperty("categoryAttribute", new View(viewRepository.getView(CategoryAttribute.class, View.LOCAL), null, false).addProperty("category").addProperty("defaultEntity", viewRepository.getView(ReferenceToEntity.class, View.LOCAL)));
TypedQuery<CategoryAttributeValue> query;
if (HasUuid.class.isAssignableFrom(metaClass.getJavaClass())) {
query = em.createQuery(format("select cav from sys$CategoryAttributeValue cav where cav.entity.%s in :ids and cav.parent is null", referenceToEntitySupport.getReferenceIdPropertyName(metaClass)), CategoryAttributeValue.class);
} else {
query = em.createQuery(format("select cav from sys$CategoryAttributeValue cav where cav.entity.%s in :ids " + "and cav.categoryAttribute.categoryEntityType = :entityType and cav.parent is null", referenceToEntitySupport.getReferenceIdPropertyName(metaClass)), CategoryAttributeValue.class);
query.setParameter("entityType", metaClass.getName());
}
query.setParameter("ids", entityIds);
query.setView(view);
List<CategoryAttributeValue> resultList = query.getResultList();
List<CategoryAttributeValue> cavsOfEntityType = resultList.stream().filter(cav -> cav.getObjectEntityValueId() != null).collect(Collectors.toList());
List<CategoryAttributeValue> cavsOfCollectionType = resultList.stream().filter(cav -> cav.getCategoryAttribute().getIsCollection()).collect(Collectors.toList());
if (cavsOfCollectionType.isEmpty()) {
loadEntityValues(cavsOfEntityType);
attributeValues.addAll(resultList);
} else {
List<CategoryAttributeValue> cavsOfCollectionTypeWithChildren = reloadCategoryAttributeValuesWithChildren(cavsOfCollectionType);
// add nested collection values to the cavsOfEntityType collection, because this collection will later be
// used for loading entity values
cavsOfCollectionTypeWithChildren.stream().filter(cav -> cav.getCategoryAttribute().getDataType() == PropertyType.ENTITY && cav.getChildValues() != null).forEach(cav -> cavsOfEntityType.addAll(cav.getChildValues()));
loadEntityValues(cavsOfEntityType);
cavsOfCollectionTypeWithChildren.stream().filter(cav -> cav.getChildValues() != null).forEach(cav -> {
List<Object> value = cav.getChildValues().stream().map(CategoryAttributeValue::getValue).collect(Collectors.toList());
cav.setTransientCollectionValue(value);
});
attributeValues.addAll(resultList.stream().filter(cav -> !cavsOfCollectionTypeWithChildren.contains(cav)).collect(Collectors.toList()));
attributeValues.addAll(cavsOfCollectionTypeWithChildren);
}
tx.commit();
}
return attributeValues;
}
Aggregations