我的这个请求与 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
.
我尝试把我的where
在Predicate 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/