gpt4 book ai didi

spring - 如何使用 Spring Data 和 QueryDSL 执行带分页的 JPAQuery

转载 作者:行者123 更新时间:2023-12-02 11:31:20 26 4
gpt4 key购买 nike

我的这个请求与 queryDSL 配合得很好:

 Iterable<AO> query_result = new JPAQuery(entityManager).from(ao)
.leftJoin( ao.lots , lot )
.leftJoin( ao.acs , ac )
.where(where).distinct()
.list(ao);

但是如果我们将它与 spring data jpa 一起使用,它的等价物是什么?

ao_respository.findAll(Predicate arg0, Pageable arg1);

因为我想返回一个Page只需 querydsl它没有实现Page没有spring data jpa .

我尝试把我的wherePredicate arg0但我遇到了这个异常

Undeclared path 'lot '. Add this path as a source to the query to be able to reference it

哪里lot声明为QLot lot = QLot.lot;

最佳答案

返回页面:

JPAQuery query = 
...
.orderBy(getOrderSpecifiers(pageable, MyEntity.class))
.limit(pageable.getPageSize())
.offset(pageable.getOffset());

long total = query.fetchCount();
List<MyEntity> content = query.fetch();
return new PageImpl<>(content, pageable, total);

我创建了这个函数来获取OrderSpecifier:

private OrderSpecifier[] getOrderSpecifiers(@NotNull Pageable pageable, @NotNull Class klass) {

// orderVariable must match the variable of FROM
String className = klass.getSimpleName();
final String orderVariable = String.valueOf(Character.toLowerCase(className.charAt(0))).concat(className.substring(1));

return pageable.getSort().stream()
.map(order -> new OrderSpecifier(
Order.valueOf(order.getDirection().toString()),
new PathBuilder(klass, orderVariable).get(order.getProperty()))
)
.toArray(OrderSpecifier[]::new);
}

关于spring - 如何使用 Spring Data 和 QueryDSL 执行带分页的 JPAQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24880680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com