本文介绍了spring-data 规范 - 返回 id 列表而不是对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取所有订阅用户的所有 ID.这是我的谓词:

i'm looking to get all ids of all subscribed users. this is my predicate:

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.有没有办法让它返回某个字段的列表?

My repository, however, only allows count, findAll and findOne. Is there a way to get it to return a list of a certain field?

推荐答案

据我所知,Spring-Data 尚不支持此功能.阅读 来自Oliver Gierke 关于相关问题.那里提到的那些问题仍然悬而未决.

As far as I know this isn't supported by Spring-Data yet. Read a response fromOliver Gierke on related question. And those issues that were mentioned there are still open.

作为替代方案,您可以创建一个 自定义存储库 并使用 TypedQuery 来实现您想要的.以下是 TypedQuery 的一些示例:http://www.objectdb.com/java/jpa/query/jpql/选择

As an alternative you can create a custom repository and use TypedQuery to achive what you want.Here are some examples of TypedQuery:http://www.objectdb.com/java/jpa/query/jpql/select

TypedQuery 示例:

Example of TypedQuery:

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

这篇关于spring-data 规范 - 返回 id 列表而不是对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 11:11