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

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

本文整理了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

@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

@Override
  public void run(EntityManager em) {
    Query query = em.createQuery("delete from sys$ScheduledExecution e where e.id in ?1");
    query.setParameter(1, subList);
    query.executeUpdate();
  }
});

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

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

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

private Query createPolymorphicQuery(EntityManager em, Class<? extends Entity> polymorphicEntityClass,
                   String attribute,
                   Entity softReference,
                   String view) {
  String tableName = getTableNameFromEntityClass(polymorphicEntityClass);
  Query query = em.createQuery("select e from " + tableName + " e where e." + attribute + " = :softReference");
  query.setParameter("softReference", softReference, false);
  if (view != null) {
    query.setView(polymorphicEntityClass, view);
  }
  return query;
}

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

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

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

protected void removeQueuedItems(List<FtsQueue> list) {
  try (Transaction tx = persistence.createTransaction()) {
    EntityManager em = persistence.getEntityManager();
    for (int i = 0; i < list.size(); i += DEL_CHUNK) {
      StringBuilder sb = new StringBuilder("delete from SYS_FTS_QUEUE where ID in (");
      List<FtsQueue> sublist = list.subList(i, Math.min(i + DEL_CHUNK, list.size()));
      for (int idx = 0; idx < sublist.size(); idx++) {
        sb.append("?");
        if (idx < sublist.size() - 1)
          sb.append(", ");
      }
      sb.append(")");
      DbTypeConverter converter = persistence.getDbTypeConverter();
      Query query = em.createNativeQuery(sb.toString());
      for (int idx = 0; idx < sublist.size(); idx++) {
        query.setParameter(idx + 1, converter.getSqlObject(sublist.get(idx).getId()));
      }
      query.executeUpdate();
    }
    tx.commit();
  }
}

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

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

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

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

代码示例来源: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

protected void removeAccessTokenFromDatabase(String accessTokenValue) {
  try (Transaction tx = persistence.getTransaction()) {
    EntityManager em = persistence.getEntityManager();
    em.createQuery("delete from sys$AccessToken t where t.tokenValue = :tokenValue")
        .setParameter("tokenValue", accessTokenValue)
        .executeUpdate();
    tx.commit();
  }
}

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

protected void removeRefreshTokenFromDatabase(String refreshTokenValue) {
  try (Transaction tx = persistence.getTransaction()) {
    EntityManager em = persistence.getEntityManager();
    em.createQuery("delete from sys$RefreshToken t where t.tokenValue = :tokenValue")
        .setParameter("tokenValue", refreshTokenValue)
        .executeUpdate();
    tx.commit();
  }
}

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

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;
}

代码示例来源: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

@Override
public void resetRememberMeTokens(List<UUID> userIds) {
  Transaction tx = persistence.getTransaction();
  try {
    EntityManager em = persistence.getEntityManager();
    Query query = em.createQuery("delete from sec$RememberMeToken rt where rt.user.id in :userIds");
    query.setParameter("userIds", userIds);
    query.executeUpdate();
    tx.commit();
  } finally {
    tx.end();
  }
}

代码示例来源: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

@Override
public void deleteStatistics(String name) {
  Transaction tx = persistence.createTransaction();
  try {
    EntityManager em = persistence.getEntityManager();
    Query q = em.createQuery("delete from sys$EntityStatistics s where s.name = ?1");
    q.setParameter(1, getOriginalOrThisEntityName(name));
    q.executeUpdate();
    tx.commit();
  } finally {
    tx.end();
  }
  flushStatisticsCache();
}

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

protected Query insertParameters(String query, String storeName, BandData parentBand, Map<String, Object> params) {
  QueryPack pack = prepareQuery(query, parentBand, params);
  boolean inserted = pack.getParams().length > 0;
  EntityManager em = persistence.getEntityManager(storeName);
  Query select = em.createQuery(pack.getQuery());
  if (inserted) {
    //insert parameters to their position
    for (QueryParameter queryParameter : pack.getParams()) {
      Object value = queryParameter.getValue();
      select.setParameter(queryParameter.getPosition(), convertParameter(value));
    }
  }
  return select;
}

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

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

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

@Override
public void changeUserPassword(UUID userId, String newPasswordHash) {
  Transaction tx = persistence.createTransaction();
  try {
    EntityManager em = persistence.getEntityManager();
    User user = em.find(User.class, userId, CHANGE_PASSWORD_VIEW);
    if (user == null) {
      throw new EntityAccessException(User.class, userId);
    }
    user.setPassword(newPasswordHash);
    user.setChangePasswordAtNextLogon(false);
    // reset remember me for user
    Query query = em.createQuery("delete from sec$RememberMeToken rt where rt.user.id=:userId");
    query.setParameter("userId", userId);
    query.executeUpdate();
    tx.commit();
  } finally {
    tx.end();
  }
}

相关文章