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

标签 spring hibernate spring-data spring-data-jpa querydsl

我的这个请求与 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/

相关文章:

应用程序上下文中的 Java bean 定义(Spring)

java - 使用 oauth 保护 Spring Restful Web 服务是一个好方法吗?

java - hibernate : self join confusion?

java - Hibernate @ManyToMany 注解支持哪些集合

spring - 是否有任何 Spring Annotation 为字段(Mongo)设置默认值?

spring - 不同集合的QueryDsl MongoRepository

java - Spring Cloud Stream 的内存中绑定(bind)器

java - JPA OneToOneToOne 映射

java - 非空属性引用 transient 值 - transient 实例必须在当前操作之前保存

java - 当我在此 Spring 应用程序中使用 JdbcTemplate BeanPropertyRowMapper 类时,为什么会出现此 InstantiationException?