Spring - data specifications - returns a list of identifiers instead of objects

I am looking to get all the IDs of all subscribers. 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 */); } }; } 

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

+4
source share
1 answer

As far as I know, this is not yet supported by Spring-Data. Read the answer from Oliver Girke on the relevant question. And the problems that were mentioned there are still open.

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

TypedQuery example:

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

All Articles