spring-data specifications - 返回id而不是object的列表

时间:2012-12-20 13:40:48

标签: spring-data

我希望获得所有订阅用户的所有ID。这是我的谓词:

public static Specification<User> userSubscribed(){
  return new Specification<User>() {
    public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
      return builder.equal(root.get("status"), 1 /* 1 = subscribed */);
    }
  };
}

但是,我的存储库只允许count,findAll和findOne。有没有办法让它返回某个字段的列表?

1 个答案:

答案 0 :(得分:4)

据我所知,Spring-Data尚不支持。在相关问题上阅读response from Oliver Gierke。那里提到的那些问题仍然存在。

作为替代方案,您可以创建custom repository并使用TypedQuery来实现您想要的效果。 以下是TypedQuery的一些示例: http://www.objectdb.com/java/jpa/query/jpql/select

TypedQuery示例:

TypedQuery<Integer> query = em.createQuery(
    "SELECT u.id FROM User AS u", Integer.class);
// ... Some predicates
List<Integer> results = query.getResultList();