Pigna
Pigna

Reputation: 1

Spring Boot 3.4 - JPA specification and Projection

From what I have seen from the docs the JpaSpecificationExecutor now has a way to handle projections using the findBy() method.

The first method does return records correctly although it is selecting all columns as found in Entity definition. That is why I want to use projection and only select the X property (part of compositeKey)

public List<String> findStockLotsBasedOnSpec(Map<java.lang.String, Object> attributes) {
        Specification<CustomAttributeTwoEntity> specification = CustomAttributeSpecification.buildSpecification(attributes);
        return findAll(specification).stream()
                .map(entity -> entity.getId().getX())
                .distinct()
                .collect(Collectors.toList());
    }

public List<XProjection> findStockLotsBasedOnSpecAndProjection(Map<String, Object> attributes) {
        Specification<CustomAttributeTwoEntity> specification = CustomAttributeSpecification.buildSpecification(attributes);
        return findBy(specification, q -> q.as(XProjection.class).all());
    }

....

public interface XProjection {
    String getId_X();
}

Is the way I use the queryFunction correct to get Projections working?

findBy(Specification<T> spec, Function<FluentQuery.FetchableFluentQuery<S>,R> queryFunction)

Upvotes: 0

Views: 30

Answers (0)

Related Questions