最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Spring Boot 3.4 - JPA specification and Projection - Stack Overflow

programmeradmin6浏览0评论

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)
发布评论

评论列表(0)

  1. 暂无评论