springspring-data-jpaspecificationscriteria-api

Spring Data JPA. How to get only a list of IDs from findAll() method


I have a very complicated model. Entity has a lot relationship and so on.

I try to use Spring Data JPA and I prepared a repository.

but when I invoke a method findAll() with specification for the object a have a performance issue because objects are very big. I know that because when I invoke a method like this:

@Query(value = "select id, name from Customer ")
List<Object[]> myFindCustomerIds();

I didn't have any problems with performance.

But when I invoke

List<Customer> findAll(); 

I had a big problem with performance.

The problem is that I need to invoke findAll method with Specifications for Customer that is why I cannot use method which returns a list of arrays of objects.

How do I write a method to find all customers with specifications for the Customer entity but which returns only IDs.

like this:

List<Long> findAll(Specification<Customer> spec);

Please help.


Solution

  • I solved the problem.

    (As a result we will have a sparse Customer object only with id and name)

    Define their own repository:

    public interface SparseCustomerRepository {
        List<Customer> findAllWithNameOnly(Specification<Customer> spec);
    }
    

    And an implementation (remember about suffix - Impl as default)

    @Service
    public class SparseCustomerRepositoryImpl implements SparseCustomerRepository {
        private final EntityManager entityManager;
    
        @Autowired
        public SparseCustomerRepositoryImpl(EntityManager entityManager) {
            this.entityManager = entityManager;
        }
    
        @Override
        public List<Customer> findAllWithNameOnly(Specification<Customer> spec) {
            CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
            CriteriaQuery<Tuple> tupleQuery = criteriaBuilder.createTupleQuery();
            Root<Customer> root = tupleQuery.from(Customer.class);
            tupleQuery.multiselect(getSelection(root, Customer_.id),
                    getSelection(root, Customer_.name));
            if (spec != null) {
                tupleQuery.where(spec.toPredicate(root, tupleQuery, criteriaBuilder));
            }
    
            List<Tuple> CustomerNames = entityManager.createQuery(tupleQuery).getResultList();
            return createEntitiesFromTuples(CustomerNames);
        }
    
        private Selection<?> getSelection(Root<Customer> root,
                SingularAttribute<Customer, ?> attribute) {
            return root.get(attribute).alias(attribute.getName());
        }
    
        private List<Customer> createEntitiesFromTuples(List<Tuple> CustomerNames) {
            List<Customer> customers = new ArrayList<>();
            for (Tuple customer : CustomerNames) {
                Customer c = new Customer();
                c.setId(customer.get(Customer_.id.getName(), Long.class));
                c.setName(customer.get(Customer_.name.getName(), String.class));
                c.add(customer);
            }
            return customers;
        }
    }