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

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

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

Query.setParameter介绍

[英]Bind an argument to a positional parameter.

In the query text, positional parameters are marked with ?N (e.g. ?1).
[中]将参数绑定到位置参数。
在查询文本中,位置参数标记为?N(例如?1)。

代码示例

代码示例来源: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. @Override
  2. public void run(EntityManager em) {
  3. Query query = em.createQuery("delete from sys$ScheduledExecution e where e.id in ?1");
  4. query.setParameter(1, subList);
  5. query.executeUpdate();
  6. }
  7. });

代码示例来源: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: com.haulmont.cuba/cuba-core

  1. @Override
  2. public long getLastFinished(ScheduledTask task) {
  3. EntityManager em = persistence.getEntityManager();
  4. Query query = em.createQuery(
  5. "select max(e.finishTime) from sys$ScheduledExecution e where e.task.id = ?1")
  6. .setParameter(1, task.getId());
  7. Date date = (Date) query.getFirstResult();
  8. return date == null ? 0 : date.getTime();
  9. }

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

  1. private Query createPolymorphicQuery(EntityManager em, Class<? extends Entity> polymorphicEntityClass,
  2. String attribute,
  3. Entity softReference,
  4. String view) {
  5. String tableName = getTableNameFromEntityClass(polymorphicEntityClass);
  6. Query query = em.createQuery("select e from " + tableName + " e where e." + attribute + " = :softReference");
  7. query.setParameter("softReference", softReference, false);
  8. if (view != null) {
  9. query.setView(polymorphicEntityClass, view);
  10. }
  11. return query;
  12. }

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

  1. @Override
  2. public void emptyFakeQueue(String entityName) {
  3. EntityManager em = persistence.getEntityManager();
  4. Query q = em.createQuery("delete from sys$FtsQueue q where q.entityName = ?1 and q.fake = true");
  5. q.setParameter(1, entityName);
  6. q.executeUpdate();
  7. }

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

  1. protected void removeQueuedItems(List<FtsQueue> list) {
  2. try (Transaction tx = persistence.createTransaction()) {
  3. EntityManager em = persistence.getEntityManager();
  4. for (int i = 0; i < list.size(); i += DEL_CHUNK) {
  5. StringBuilder sb = new StringBuilder("delete from SYS_FTS_QUEUE where ID in (");
  6. List<FtsQueue> sublist = list.subList(i, Math.min(i + DEL_CHUNK, list.size()));
  7. for (int idx = 0; idx < sublist.size(); idx++) {
  8. sb.append("?");
  9. if (idx < sublist.size() - 1)
  10. sb.append(", ");
  11. }
  12. sb.append(")");
  13. DbTypeConverter converter = persistence.getDbTypeConverter();
  14. Query query = em.createNativeQuery(sb.toString());
  15. for (int idx = 0; idx < sublist.size(); idx++) {
  16. query.setParameter(idx + 1, converter.getSqlObject(sublist.get(idx).getId()));
  17. }
  18. query.executeUpdate();
  19. }
  20. tx.commit();
  21. }
  22. }

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

  1. @Override
  2. public void emptyQueue(String entityName) {
  3. EntityManager em = persistence.getEntityManager();
  4. Query q = em.createQuery("delete from sys$FtsQueue q where q.entityName = ?1");
  5. q.setParameter(1, entityName);
  6. q.executeUpdate();
  7. }

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

  1. protected <T extends Entity> T findWithViews(MetaClass metaClass, Object id, List<View> views) {
  2. Object realId = getRealId(id);
  3. log.debug("find {} by id={}, views={}", metaClass.getJavaClass().getSimpleName(), realId, views);
  4. String pkName = metadata.getTools().getPrimaryKeyName(metaClass);
  5. if (pkName == null)
  6. throw new IllegalStateException("Cannot determine PK name for entity " + metaClass);
  7. Query query = createQuery(String.format("select e from %s e where e.%s = ?1", metaClass.getName(), pkName));
  8. ((QueryImpl) query).setSingleResultExpected(true);
  9. query.setParameter(1, realId);
  10. for (View view : views) {
  11. query.addView(view);
  12. }
  13. //noinspection unchecked
  14. return (T) query.getFirstResult();
  15. }

代码示例来源: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. protected void removeAccessTokenFromDatabase(String accessTokenValue) {
  2. try (Transaction tx = persistence.getTransaction()) {
  3. EntityManager em = persistence.getEntityManager();
  4. em.createQuery("delete from sys$AccessToken t where t.tokenValue = :tokenValue")
  5. .setParameter("tokenValue", accessTokenValue)
  6. .executeUpdate();
  7. tx.commit();
  8. }
  9. }

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

  1. protected void removeRefreshTokenFromDatabase(String refreshTokenValue) {
  2. try (Transaction tx = persistence.getTransaction()) {
  3. EntityManager em = persistence.getEntityManager();
  4. em.createQuery("delete from sys$RefreshToken t where t.tokenValue = :tokenValue")
  5. .setParameter("tokenValue", refreshTokenValue)
  6. .executeUpdate();
  7. tx.commit();
  8. }
  9. }

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

  1. protected boolean referenceExists(String entityName, MetaProperty property) {
  2. String template = property.getRange().getCardinality().isMany() ?
  3. "select count(e) from %s e join e.%s c where c." + primaryKeyName + "= ?1" :
  4. "select count(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. query.setMaxResults(1);
  9. Long count = (Long) query.getSingleResult();
  10. return count > 0;
  11. }

代码示例来源: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. @Override
  2. public void resetRememberMeTokens(List<UUID> userIds) {
  3. Transaction tx = persistence.getTransaction();
  4. try {
  5. EntityManager em = persistence.getEntityManager();
  6. Query query = em.createQuery("delete from sec$RememberMeToken rt where rt.user.id in :userIds");
  7. query.setParameter("userIds", userIds);
  8. query.executeUpdate();
  9. tx.commit();
  10. } finally {
  11. tx.end();
  12. }
  13. }

代码示例来源: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. @Override
  2. public void deleteStatistics(String name) {
  3. Transaction tx = persistence.createTransaction();
  4. try {
  5. EntityManager em = persistence.getEntityManager();
  6. Query q = em.createQuery("delete from sys$EntityStatistics s where s.name = ?1");
  7. q.setParameter(1, getOriginalOrThisEntityName(name));
  8. q.executeUpdate();
  9. tx.commit();
  10. } finally {
  11. tx.end();
  12. }
  13. flushStatisticsCache();
  14. }

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

  1. protected Query insertParameters(String query, String storeName, BandData parentBand, Map<String, Object> params) {
  2. QueryPack pack = prepareQuery(query, parentBand, params);
  3. boolean inserted = pack.getParams().length > 0;
  4. EntityManager em = persistence.getEntityManager(storeName);
  5. Query select = em.createQuery(pack.getQuery());
  6. if (inserted) {
  7. //insert parameters to their position
  8. for (QueryParameter queryParameter : pack.getParams()) {
  9. Object value = queryParameter.getValue();
  10. select.setParameter(queryParameter.getPosition(), convertParameter(value));
  11. }
  12. }
  13. return select;
  14. }

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

  1. protected Entity getReference(Entity entity, MetaProperty property) {
  2. if (PersistenceHelper.isLoaded(entity, property.getName()))
  3. return entity.getValue(property.getName());
  4. else {
  5. Query query = entityManager.createQuery(
  6. "select e." + property.getName() + " from " + entity.getMetaClass().getName() + " e where e." + primaryKeyName + " = ?1");
  7. query.setParameter(1, entity.getId());
  8. Object refEntity = query.getFirstResult();
  9. return (Entity) refEntity;
  10. }
  11. }

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

  1. @Override
  2. public void changeUserPassword(UUID userId, String newPasswordHash) {
  3. Transaction tx = persistence.createTransaction();
  4. try {
  5. EntityManager em = persistence.getEntityManager();
  6. User user = em.find(User.class, userId, CHANGE_PASSWORD_VIEW);
  7. if (user == null) {
  8. throw new EntityAccessException(User.class, userId);
  9. }
  10. user.setPassword(newPasswordHash);
  11. user.setChangePasswordAtNextLogon(false);
  12. // reset remember me for user
  13. Query query = em.createQuery("delete from sec$RememberMeToken rt where rt.user.id=:userId");
  14. query.setParameter("userId", userId);
  15. query.executeUpdate();
  16. tx.commit();
  17. } finally {
  18. tx.end();
  19. }
  20. }

相关文章