1

Play Framework / JPA 中是否有一个快捷方式来保存手动构建此查询?

public List<User> getAllExceptThese(Collection<String> emails) {
    checkArgument(!emails.isEmpty());

    StringBuilder query = new StringBuilder("email not in (");
    boolean first = true;
    for (String email : emails) {
        if (!first) {
            query.append(", ");
        }
        first = false;
        query.append("?");
    }
    query.append(")");

    return findAll(query.toString(), emails.toArray());
}
4

1 回答 1

2

我不知道播放框架,但如果可以创建 JPQL 查询,那么构建一个Query对象并使用它来插入集合怎么样......

  public List<User> getAllExceptThese(Collection<String> emails) {
            checkArgument(!emails.isEmpty());

            String queryStr = "FROM User u WHERE u.email NOT IN (:excludedEmails)";

            Query query = entityManager.createQuery(queryStr);
            query.setParameter("excludedEmails", emails);
            return (List<User>)query.getResultList();
  }
于 2012-08-08T17:23:22.433 回答