com.haulmont.cuba.core.Query.getResultList()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(12.4k)|赞(0)|评价(0)|浏览(326)

本文整理了Java中com.haulmont.cuba.core.Query.getResultList方法的一些代码示例,展示了Query.getResultList的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.getResultList方法的具体详情如下:
包路径:com.haulmont.cuba.core.Query
类名称:Query
方法名:getResultList

Query.getResultList介绍

[英]Execute a SELECT query and return the query results as a List.
[中]执行SELECT查询并将查询结果作为列表返回。

代码示例

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. @Override
  2. @Transactional(timeout = 2)
  3. public String executeSelectSql(String sql) {
  4. checkTestMode();
  5. log.info("started: " + sql);
  6. EntityManager em = persistence.getEntityManager();
  7. Query query = em.createNativeQuery(sql);
  8. query.getResultList();
  9. log.info("finished: " + sql);
  10. return "Done";
  11. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. @Nullable
  2. protected User loadUser(String login) throws LoginException {
  3. if (login == null) {
  4. throw new IllegalArgumentException("Login is null");
  5. }
  6. EntityManager em = persistence.getEntityManager();
  7. String queryStr = "select u from sec$User u where u.loginLowerCase = ?1 and (u.active = true or u.active is null)";
  8. Query q = em.createQuery(queryStr);
  9. q.setParameter(1, login.toLowerCase());
  10. List list = q.getResultList();
  11. if (list.isEmpty()) {
  12. log.debug("Unable to find user: {}", login);
  13. return null;
  14. } else {
  15. //noinspection UnnecessaryLocalVariable
  16. User user = (User) list.get(0);
  17. return user;
  18. }
  19. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. protected synchronized List<ScheduledTask> getTasks() {
  2. log.trace("Read all active tasks from DB and lock them");
  3. EntityManager em = persistence.getEntityManager();
  4. Query query = em.createQuery("select t from sys$ScheduledTask t where t.active = true");
  5. query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
  6. return query.getResultList();
  7. }
  8. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. @SuppressWarnings("unchecked")
  2. protected <E extends Entity> List<E> executeQuery(Query query, boolean singleResult) {
  3. List<E> list;
  4. try {
  5. if (singleResult) {
  6. try {
  7. E result = (E) query.getSingleResult();
  8. list = new ArrayList<>(1);
  9. list.add(result);
  10. } catch (NoResultException e) {
  11. list = Collections.emptyList();
  12. }
  13. } else {
  14. list = query.getResultList();
  15. }
  16. } catch (javax.persistence.PersistenceException e) {
  17. if (e.getCause() instanceof org.eclipse.persistence.exceptions.QueryException
  18. && e.getMessage() != null
  19. && e.getMessage().contains("Fetch group cannot be set on report query")) {
  20. throw new DevelopmentException("DataManager cannot execute query for single attributes");
  21. } else {
  22. throw e;
  23. }
  24. }
  25. return list;
  26. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. @Override
  2. public boolean isLastExecutionFinished(ScheduledTask task, long now) {
  3. EntityManager em = persistence.getEntityManager();
  4. Query query = em.createQuery(
  5. "select e.finishTime from sys$ScheduledExecution e where e.task.id = ?1 and e.startTime = ?2");
  6. query.setParameter(1, task.getId());
  7. query.setParameter(2, task.getLastStartTime());
  8. List list = query.getResultList();
  9. if (list.isEmpty() || list.get(0) == null) {
  10. // Execution finish was not registered for some reason, so using timeout value or just return false
  11. boolean result = task.getTimeout() != null
  12. && (task.getLastStart() + task.getTimeout() * 1000) <= now;
  13. if (result)
  14. log.trace(task + ": considered finished because of timeout");
  15. else
  16. log.trace(task + ": not finished and not timed out");
  17. return result;
  18. }
  19. Date date = (Date) list.get(0);
  20. log.trace("{} : finished at {}", task, date.getTime());
  21. return true;
  22. }

代码示例来源:origin: de.diedavids.cuba.entitysoftreference/entity-soft-reference-core

  1. @Override
  2. public <T extends Entity> Collection<T> loadEntitiesForSoftReference(Class<T> polymorphicEntityClass,
  3. Entity softReference,
  4. String attribute,
  5. String view) {
  6. Transaction tx = persistence.createTransaction();
  7. EntityManager em = persistence.getEntityManager();
  8. Query query = createPolymorphicQuery(em, polymorphicEntityClass, attribute, softReference, view);
  9. List result = query.getResultList();
  10. tx.commit();
  11. return result;
  12. }

代码示例来源:origin: com.haulmont.reports/reports-core

  1. @Override
  2. public List<Map<String, Object>> loadData(ReportQuery reportQuery, BandData parentBand, Map<String, Object> params) {
  3. String storeName = StoreUtils.getStoreName(reportQuery);
  4. String query = reportQuery.getScript();
  5. if (StringUtils.isBlank(query)) {
  6. return Collections.emptyList();
  7. }
  8. try (Transaction tx = persistence.createTransaction(storeName)) {
  9. if (Boolean.TRUE.equals(reportQuery.getProcessTemplate())) {
  10. query = processQueryTemplate(query, parentBand, params);
  11. }
  12. List<OutputValue> outputParameters = parseQueryOutputParametersNames(query);
  13. query = query.replaceAll("(?i)" + ALIAS_PATTERN + ",", ",");//replaces [as alias_name], entries except last
  14. query = query.replaceAll("(?i)" + ALIAS_PATTERN, " ");//replaces last [as alias_name] entry
  15. Query select = insertParameters(trimQuery(query), storeName, parentBand, params);
  16. List queryResult = select.getResultList();
  17. tx.commit();
  18. if (queryResult.size() > 0 && queryResult.get(0) instanceof Entity) {
  19. List<Map<String, Object>> wrappedResults = new ArrayList<>();
  20. for (Object theResult : queryResult) {
  21. wrappedResults.add(new EntityMap((Entity) theResult));
  22. }
  23. return wrappedResults;
  24. } else {
  25. return fillOutputData(queryResult, outputParameters);
  26. }
  27. } catch (Throwable e) {
  28. throw new DataLoadingException(String.format("An error occurred while loading data for data set [%s]", reportQuery.getName()), e);
  29. }
  30. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. @Override
  2. public List<String> getAvailableUsers() {
  3. List<String> result = new ArrayList<>();
  4. Transaction tx = persistence.createTransaction();
  5. try {
  6. EntityManager em = persistence.getEntityManager();
  7. Query query = em.createQuery("select u from sec$User u");
  8. List<User> userList = query.getResultList();
  9. for (User user : userList) {
  10. result.add(user.getLogin());
  11. }
  12. tx.commit();
  13. } finally {
  14. tx.end();
  15. }
  16. return result;
  17. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. @Override
  2. public List<String> getSessionAttributeNames(UUID groupId) {
  3. Preconditions.checkNotNullArgument(groupId, "groupId is null");
  4. checkPermission(SessionAttribute.class, EntityOp.READ);
  5. checkUpdatePermission(Group.class);
  6. checkUpdatePermission(Constraint.class);
  7. Set<String> attributes;
  8. try (Transaction tx = persistence.createTransaction()) {
  9. EntityManager em = persistence.getEntityManager();
  10. Query query = em.createQuery("select a.name from sec$SessionAttribute a where a.group.id = ?1");
  11. query.setParameter(1, groupId);
  12. //noinspection unchecked
  13. attributes = new HashSet<>(query.getResultList());
  14. query = em.createQuery("select a.name from sec$GroupHierarchy h join h.parent.sessionAttributes a where h.group.id = ?1");
  15. query.setParameter(1, groupId);
  16. //noinspection unchecked
  17. attributes.addAll(query.getResultList());
  18. tx.commit();
  19. }
  20. return new ArrayList<>(attributes);
  21. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. SequenceSupport sequenceSupport = getSequenceSupport(sequence);
  2. Query query = em.createNativeQuery(sequenceSupport.sequenceExistsSql(sequenceName));
  3. List list = query.getResultList();
  4. if (list.isEmpty()) {
  5. query = em.createNativeQuery(sequenceSupport.createSequenceSql(sequenceName, sequence.getStartValue(), sequence.getIncrement()));

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. protected void cascade(String entityName, MetaProperty property) {
  2. String template = property.getRange().getCardinality().isMany() ?
  3. "select e from %s e join e.%s c where c." + primaryKeyName + " = ?1" :
  4. "select e from %s e where e.%s." + primaryKeyName + " = ?1";
  5. String qstr = String.format(template, entityName, property.getName());
  6. Query query = entityManager.createQuery(qstr);
  7. query.setParameter(1, entity.getId());
  8. List<Entity> list = query.getResultList();
  9. for (Entity e : list) {
  10. entityManager.remove(e);
  11. }
  12. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. .setParameter("ids", ids)
  2. .setView(metaClass.getJavaClass(), View.MINIMAL)
  3. .getResultList();

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. query.setParameter(2, Integer.parseInt(maxPeriod) * 3600);
  2. list = query.getResultList();

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. protected Collection<Entity> getCollection(MetaProperty property) {
  2. MetaProperty inverseProperty = property.getInverse();
  3. if (inverseProperty == null) {
  4. log.warn("Inverse property not found for property {}", property);
  5. Collection<Entity> value = entity.getValue(property.getName());
  6. return value == null ? Collections.EMPTY_LIST : value;
  7. }
  8. String invPropName = inverseProperty.getName();
  9. String qlStr = "select e from " + property.getRange().asClass().getName() + " e where e." + invPropName + "." +
  10. primaryKeyName + " = ?1";
  11. Query query = entityManager.createQuery(qlStr);
  12. query.setParameter(1, entity.getId());
  13. List<Entity> list = query.getResultList();
  14. // If the property is not loaded, it means it was not modified and further check is not needed
  15. if (!PersistenceHelper.isLoaded(entity, property.getName())) {
  16. return list;
  17. }
  18. // Check whether the collection items still belong to the master entity, because they could be changed in the
  19. // current transaction that did not affect the database yet
  20. List<Entity> result = new ArrayList<>(list.size());
  21. for (Entity item : list) {
  22. Entity master = item.getValue(invPropName);
  23. if (entity.equals(master))
  24. result.add(item);
  25. }
  26. return result;
  27. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. @Override
  2. public Role copyRole(UUID roleId) {
  3. checkNotNullArgument(roleId, "Null access role id");
  4. checkUpdatePermission(Role.class);
  5. Role clone;
  6. Transaction tx = persistence.getTransaction();
  7. try {
  8. EntityManager em = persistence.getEntityManager();
  9. Query roleNamesQuery = em.createQuery("select g.name from sec$Role g");
  10. @SuppressWarnings("unchecked")
  11. Set<String> roleNames = new HashSet<>(roleNamesQuery.getResultList());
  12. Role role = em.find(Role.class, roleId, ROLE_COPY_VIEW);
  13. if (role == null)
  14. throw new IllegalStateException("Unable to find specified role with id: " + roleId);
  15. clone = cloneRole(role, roleNames, em);
  16. clone.setDefaultRole(false);
  17. tx.commit();
  18. } finally {
  19. tx.end();
  20. }
  21. return clone;
  22. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. @Override
  2. public Group copyAccessGroup(UUID accessGroupId) {
  3. checkNotNullArgument(accessGroupId, "Null access group id");
  4. checkUpdatePermission(Group.class);
  5. Group clone;
  6. Transaction tx = persistence.getTransaction();
  7. try {
  8. EntityManager em = persistence.getEntityManager();
  9. Query groupNamesQuery = em.createQuery("select g.name from sec$Group g");
  10. @SuppressWarnings("unchecked")
  11. Set<String> groupNames = new HashSet<>(groupNamesQuery.getResultList());
  12. Group accessGroup = em.find(Group.class, accessGroupId, GROUP_COPY_VIEW);
  13. if (accessGroup == null)
  14. throw new IllegalStateException("Unable to find specified access group with id: " + accessGroupId);
  15. clone = cloneGroup(accessGroup, accessGroup.getParent(), groupNames, em);
  16. tx.commit();
  17. } finally {
  18. tx.end();
  19. }
  20. return clone;
  21. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. "from sec$ScreenHistory h group by h.user.id having count(h.id) > ?1");
  2. q.setParameter(1, MAX_RECORDS);
  3. List<Object[]> userList = q.getResultList();
  4. tx.commitRetaining();

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. protected boolean isCollectionEmpty(MetaProperty property) {
  2. MetaProperty inverseProperty = property.getInverse();
  3. if (inverseProperty == null) {
  4. log.warn("Inverse property not found for property {}", property);
  5. Collection<Entity> value = entity.getValue(property.getName());
  6. return value == null || value.isEmpty();
  7. }
  8. String invPropName = inverseProperty.getName();
  9. String collectionPkName = metadata.getTools().getPrimaryKeyName(property.getRange().asClass());
  10. String qlStr = "select e." + collectionPkName + " from " + property.getRange().asClass().getName() +
  11. " e where e." + invPropName + "." + primaryKeyName + " = ?1";
  12. Query query = entityManager.createQuery(qlStr);
  13. query.setParameter(1, entity.getId());
  14. query.setMaxResults(1);
  15. List<Entity> list = query.getResultList();
  16. return list.isEmpty();
  17. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. protected void unlink(String entityName, MetaProperty property) {
  2. if (metadata.getTools().isOwningSide(property)) {
  3. String template = property.getRange().getCardinality().isMany() ?
  4. "select e from %s e join e.%s c where c." + primaryKeyName + " = ?1" :
  5. "select e from %s e where e.%s." + primaryKeyName + " = ?1";
  6. String qstr = String.format(template, entityName, property.getName());
  7. Query query = entityManager.createQuery(qstr);
  8. query.setParameter(1, entity.getId());
  9. List<Entity> list = query.getResultList();
  10. for (Entity e : list) {
  11. if (property.getRange().getCardinality().isMany()) {
  12. Collection collection = e.getValue(property.getName());
  13. if (collection != null) {
  14. collection.removeIf(o -> entity.equals(o));
  15. }
  16. } else {
  17. setReferenceNull(e, property);
  18. }
  19. }
  20. } else {
  21. MetaProperty inverseProp = property.getInverse();
  22. if (inverseProp != null && inverseProp.getDomain().equals(metaClass)) {
  23. setReferenceNull(entity, inverseProp);
  24. }
  25. }
  26. }
  27. }

代码示例来源:origin: com.haulmont.cuba/cuba-core

  1. security.setQueryParam(query, paramName);
  2. List resultList = query.getResultList();
  3. tx.commit();

相关文章