本文整理了Java中com.haulmont.cuba.core.Query.getResultList
方法的一些代码示例,展示了Query.getResultList
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.getResultList
方法的具体详情如下:
包路径:com.haulmont.cuba.core.Query
类名称:Query
方法名:getResultList
[英]Execute a SELECT query and return the query results as a List.
[中]执行SELECT查询并将查询结果作为列表返回。
代码示例来源:origin: com.haulmont.cuba/cuba-core
@Override
@Transactional(timeout = 2)
public String executeSelectSql(String sql) {
checkTestMode();
log.info("started: " + sql);
EntityManager em = persistence.getEntityManager();
Query query = em.createNativeQuery(sql);
query.getResultList();
log.info("finished: " + sql);
return "Done";
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
@Nullable
protected User loadUser(String login) throws LoginException {
if (login == null) {
throw new IllegalArgumentException("Login is null");
}
EntityManager em = persistence.getEntityManager();
String queryStr = "select u from sec$User u where u.loginLowerCase = ?1 and (u.active = true or u.active is null)";
Query q = em.createQuery(queryStr);
q.setParameter(1, login.toLowerCase());
List list = q.getResultList();
if (list.isEmpty()) {
log.debug("Unable to find user: {}", login);
return null;
} else {
//noinspection UnnecessaryLocalVariable
User user = (User) list.get(0);
return user;
}
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
protected synchronized List<ScheduledTask> getTasks() {
log.trace("Read all active tasks from DB and lock them");
EntityManager em = persistence.getEntityManager();
Query query = em.createQuery("select t from sys$ScheduledTask t where t.active = true");
query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
return query.getResultList();
}
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
@SuppressWarnings("unchecked")
protected <E extends Entity> List<E> executeQuery(Query query, boolean singleResult) {
List<E> list;
try {
if (singleResult) {
try {
E result = (E) query.getSingleResult();
list = new ArrayList<>(1);
list.add(result);
} catch (NoResultException e) {
list = Collections.emptyList();
}
} else {
list = query.getResultList();
}
} catch (javax.persistence.PersistenceException e) {
if (e.getCause() instanceof org.eclipse.persistence.exceptions.QueryException
&& e.getMessage() != null
&& e.getMessage().contains("Fetch group cannot be set on report query")) {
throw new DevelopmentException("DataManager cannot execute query for single attributes");
} else {
throw e;
}
}
return list;
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
@Override
public boolean isLastExecutionFinished(ScheduledTask task, long now) {
EntityManager em = persistence.getEntityManager();
Query query = em.createQuery(
"select e.finishTime from sys$ScheduledExecution e where e.task.id = ?1 and e.startTime = ?2");
query.setParameter(1, task.getId());
query.setParameter(2, task.getLastStartTime());
List list = query.getResultList();
if (list.isEmpty() || list.get(0) == null) {
// Execution finish was not registered for some reason, so using timeout value or just return false
boolean result = task.getTimeout() != null
&& (task.getLastStart() + task.getTimeout() * 1000) <= now;
if (result)
log.trace(task + ": considered finished because of timeout");
else
log.trace(task + ": not finished and not timed out");
return result;
}
Date date = (Date) list.get(0);
log.trace("{} : finished at {}", task, date.getTime());
return true;
}
代码示例来源:origin: de.diedavids.cuba.entitysoftreference/entity-soft-reference-core
@Override
public <T extends Entity> Collection<T> loadEntitiesForSoftReference(Class<T> polymorphicEntityClass,
Entity softReference,
String attribute,
String view) {
Transaction tx = persistence.createTransaction();
EntityManager em = persistence.getEntityManager();
Query query = createPolymorphicQuery(em, polymorphicEntityClass, attribute, softReference, view);
List result = query.getResultList();
tx.commit();
return result;
}
代码示例来源:origin: com.haulmont.reports/reports-core
@Override
public List<Map<String, Object>> loadData(ReportQuery reportQuery, BandData parentBand, Map<String, Object> params) {
String storeName = StoreUtils.getStoreName(reportQuery);
String query = reportQuery.getScript();
if (StringUtils.isBlank(query)) {
return Collections.emptyList();
}
try (Transaction tx = persistence.createTransaction(storeName)) {
if (Boolean.TRUE.equals(reportQuery.getProcessTemplate())) {
query = processQueryTemplate(query, parentBand, params);
}
List<OutputValue> outputParameters = parseQueryOutputParametersNames(query);
query = query.replaceAll("(?i)" + ALIAS_PATTERN + ",", ",");//replaces [as alias_name], entries except last
query = query.replaceAll("(?i)" + ALIAS_PATTERN, " ");//replaces last [as alias_name] entry
Query select = insertParameters(trimQuery(query), storeName, parentBand, params);
List queryResult = select.getResultList();
tx.commit();
if (queryResult.size() > 0 && queryResult.get(0) instanceof Entity) {
List<Map<String, Object>> wrappedResults = new ArrayList<>();
for (Object theResult : queryResult) {
wrappedResults.add(new EntityMap((Entity) theResult));
}
return wrappedResults;
} else {
return fillOutputData(queryResult, outputParameters);
}
} catch (Throwable e) {
throw new DataLoadingException(String.format("An error occurred while loading data for data set [%s]", reportQuery.getName()), e);
}
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
@Override
public List<String> getAvailableUsers() {
List<String> result = new ArrayList<>();
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
Query query = em.createQuery("select u from sec$User u");
List<User> userList = query.getResultList();
for (User user : userList) {
result.add(user.getLogin());
}
tx.commit();
} finally {
tx.end();
}
return result;
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
@Override
public List<String> getSessionAttributeNames(UUID groupId) {
Preconditions.checkNotNullArgument(groupId, "groupId is null");
checkPermission(SessionAttribute.class, EntityOp.READ);
checkUpdatePermission(Group.class);
checkUpdatePermission(Constraint.class);
Set<String> attributes;
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
Query query = em.createQuery("select a.name from sec$SessionAttribute a where a.group.id = ?1");
query.setParameter(1, groupId);
//noinspection unchecked
attributes = new HashSet<>(query.getResultList());
query = em.createQuery("select a.name from sec$GroupHierarchy h join h.parent.sessionAttributes a where h.group.id = ?1");
query.setParameter(1, groupId);
//noinspection unchecked
attributes.addAll(query.getResultList());
tx.commit();
}
return new ArrayList<>(attributes);
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
SequenceSupport sequenceSupport = getSequenceSupport(sequence);
Query query = em.createNativeQuery(sequenceSupport.sequenceExistsSql(sequenceName));
List list = query.getResultList();
if (list.isEmpty()) {
query = em.createNativeQuery(sequenceSupport.createSequenceSql(sequenceName, sequence.getStartValue(), sequence.getIncrement()));
代码示例来源:origin: com.haulmont.cuba/cuba-core
protected void cascade(String entityName, MetaProperty property) {
String template = property.getRange().getCardinality().isMany() ?
"select e from %s e join e.%s c where c." + primaryKeyName + " = ?1" :
"select 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());
List<Entity> list = query.getResultList();
for (Entity e : list) {
entityManager.remove(e);
}
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
.setParameter("ids", ids)
.setView(metaClass.getJavaClass(), View.MINIMAL)
.getResultList();
代码示例来源:origin: com.haulmont.cuba/cuba-core
query.setParameter(2, Integer.parseInt(maxPeriod) * 3600);
list = query.getResultList();
代码示例来源:origin: com.haulmont.cuba/cuba-core
protected Collection<Entity> getCollection(MetaProperty property) {
MetaProperty inverseProperty = property.getInverse();
if (inverseProperty == null) {
log.warn("Inverse property not found for property {}", property);
Collection<Entity> value = entity.getValue(property.getName());
return value == null ? Collections.EMPTY_LIST : value;
}
String invPropName = inverseProperty.getName();
String qlStr = "select e from " + property.getRange().asClass().getName() + " e where e." + invPropName + "." +
primaryKeyName + " = ?1";
Query query = entityManager.createQuery(qlStr);
query.setParameter(1, entity.getId());
List<Entity> list = query.getResultList();
// If the property is not loaded, it means it was not modified and further check is not needed
if (!PersistenceHelper.isLoaded(entity, property.getName())) {
return list;
}
// Check whether the collection items still belong to the master entity, because they could be changed in the
// current transaction that did not affect the database yet
List<Entity> result = new ArrayList<>(list.size());
for (Entity item : list) {
Entity master = item.getValue(invPropName);
if (entity.equals(master))
result.add(item);
}
return result;
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
@Override
public Role copyRole(UUID roleId) {
checkNotNullArgument(roleId, "Null access role id");
checkUpdatePermission(Role.class);
Role clone;
Transaction tx = persistence.getTransaction();
try {
EntityManager em = persistence.getEntityManager();
Query roleNamesQuery = em.createQuery("select g.name from sec$Role g");
@SuppressWarnings("unchecked")
Set<String> roleNames = new HashSet<>(roleNamesQuery.getResultList());
Role role = em.find(Role.class, roleId, ROLE_COPY_VIEW);
if (role == null)
throw new IllegalStateException("Unable to find specified role with id: " + roleId);
clone = cloneRole(role, roleNames, em);
clone.setDefaultRole(false);
tx.commit();
} finally {
tx.end();
}
return clone;
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
@Override
public Group copyAccessGroup(UUID accessGroupId) {
checkNotNullArgument(accessGroupId, "Null access group id");
checkUpdatePermission(Group.class);
Group clone;
Transaction tx = persistence.getTransaction();
try {
EntityManager em = persistence.getEntityManager();
Query groupNamesQuery = em.createQuery("select g.name from sec$Group g");
@SuppressWarnings("unchecked")
Set<String> groupNames = new HashSet<>(groupNamesQuery.getResultList());
Group accessGroup = em.find(Group.class, accessGroupId, GROUP_COPY_VIEW);
if (accessGroup == null)
throw new IllegalStateException("Unable to find specified access group with id: " + accessGroupId);
clone = cloneGroup(accessGroup, accessGroup.getParent(), groupNames, em);
tx.commit();
} finally {
tx.end();
}
return clone;
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
"from sec$ScreenHistory h group by h.user.id having count(h.id) > ?1");
q.setParameter(1, MAX_RECORDS);
List<Object[]> userList = q.getResultList();
tx.commitRetaining();
代码示例来源:origin: com.haulmont.cuba/cuba-core
protected boolean isCollectionEmpty(MetaProperty property) {
MetaProperty inverseProperty = property.getInverse();
if (inverseProperty == null) {
log.warn("Inverse property not found for property {}", property);
Collection<Entity> value = entity.getValue(property.getName());
return value == null || value.isEmpty();
}
String invPropName = inverseProperty.getName();
String collectionPkName = metadata.getTools().getPrimaryKeyName(property.getRange().asClass());
String qlStr = "select e." + collectionPkName + " from " + property.getRange().asClass().getName() +
" e where e." + invPropName + "." + primaryKeyName + " = ?1";
Query query = entityManager.createQuery(qlStr);
query.setParameter(1, entity.getId());
query.setMaxResults(1);
List<Entity> list = query.getResultList();
return list.isEmpty();
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
protected void unlink(String entityName, MetaProperty property) {
if (metadata.getTools().isOwningSide(property)) {
String template = property.getRange().getCardinality().isMany() ?
"select e from %s e join e.%s c where c." + primaryKeyName + " = ?1" :
"select 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());
List<Entity> list = query.getResultList();
for (Entity e : list) {
if (property.getRange().getCardinality().isMany()) {
Collection collection = e.getValue(property.getName());
if (collection != null) {
collection.removeIf(o -> entity.equals(o));
}
} else {
setReferenceNull(e, property);
}
}
} else {
MetaProperty inverseProp = property.getInverse();
if (inverseProp != null && inverseProp.getDomain().equals(metaClass)) {
setReferenceNull(entity, inverseProp);
}
}
}
}
代码示例来源:origin: com.haulmont.cuba/cuba-core
security.setQueryParam(query, paramName);
List resultList = query.getResultList();
tx.commit();
内容来源于网络,如有侵权,请联系作者删除!