当前位置: 首页 > 知识库问答 >
问题:

将Spring Data JPA与QueryDsl一起使用,分页查询会中断排序

轩辕源
2023-03-14

给定以下查询,我的结果按顺序号降序排序。当试图返回分页结果时,排序似乎中断了。我尝试过不使用。orderby()表达式并在QPageRequest中添加QSort实例。我尝试过直接调用applySorting()方法并传递一个QSort,但是对于所有密集的目的来说,分页调用破坏了我尝试的任何排序。

我使用的是QueryDsl 3.3.4和Spring Data JPA 1.6.4。由Spring IO BOM 1.0.3导入。

public List<Object[]> findAll(PersonProfile personProfile) {
    QOrderStop shipper = new QOrderStop("shipper");
    QOrderStop consignee = new QOrderStop("consignee");
    JPQLQuery query = this.from(order)
            .leftJoin(order.orderStops, shipper).on(shipper.originalStopSequence.eq(BigDecimal.valueOf(1)))
            .leftJoin(order.orderStops, consignee).on(consignee.originalStopSequence.intValue().eq(order.orderStops.size()))
            .where(order.company.eq(company).and(OrderExpressions.customerCodePredicate(order, personProfile)))
            .orderBy(order.number.desc());

    //this.getQuerydsl().applyPagination(new QPageRequest(0, 10), query);

    return query
            .list(
                    order.number, order.status, order.shipperReferenceNumber, order.consigneeReferenceNumber,
                    order.shipperCustomer.billingCity, order.shipperCustomer.state,
                    shipper.latestScheduleDate, shipper.latestScheduleTime,
                    order.consigneeCustomer.billingCity, order.consigneeCustomer.state,
                    consignee.latestScheduleDate, consignee.latestScheduleTime
            ).stream()
            .map(Tuple::toArray)
            .collect(toList());
}
select order1
from Order order1
  left join order1.orderStops as shipper on shipper.originalStopSequence = ?1
  left join order1.orderStops as consignee on cast(consignee.originalStopSequence integer) = size(order1.orderStops)
where order1.company = ?2 and (order1.customer = ?3 or order1.billTo in ?4 or order1.shipper in ?5 or order1.consignee in ?5)
order by order1.number desc
select order1
from Order order1
  left join order1.orderStops as shipper on shipper.originalStopSequence = ?1
  left join order1.orderStops as consignee on cast(consignee.originalStopSequence integer) = size(order1.orderStops)
where order1.company = ?2 and (order1.customer = ?3 or order1.billTo in ?4 or order1.shipper in ?5 or order1.consignee in ?5)
order by order1.number desc
SELECT * FROM (SELECT * FROM (SELECT EL_TEMP.*, ROWNUMBER() OVER() AS EL_ROWNM FROM (SELECT t0.OHOR# AS a1, t0.OHORST AS a2, t0.OHSRF# AS a3, t0.OHCRF# AS a4, t1.CUBCTY AS a5, t1.CUBST AS a6, t2.BSLSDT AS a7, t2.BSLSTM AS a8, t3.CUBCTY AS a9, t3.CUBST AS a10, t4.BSLSDT AS a11, t4.BSLSTM AS a12 FROM LIB.DPORDHP t0 LEFT OUTER JOIN LIB.DPORDSP t2 ON (((t2.BSOR# = t0.OHOR#) AND ((t2.BSOL# = t0.OHORS#) AND (t2.BSCM = t0.OHCM))) AND (t2.BSRSQS = ?)) LEFT OUTER JOIN LIB.DPORDSP t4 ON (((t4.BSOR# = t0.OHOR#) AND ((t4.BSOL# = t0.OHORS#) AND (t4.BSCM = t0.OHCM))) AND (CAST(t4.BSRSQS AS integer) = (SELECT COUNT(t5.BSOR#) FROM LIB.DPORDSP t5 WHERE ((t5.BSOR# = t0.OHOR#) AND ((t5.BSOL# = t0.OHORS#) AND (t5.BSCM = t0.OHCM)))))), LIB.SWCUSTP t3, LIB.SWCUSTP t1 WHERE (((t0.OHCM = ?) AND ((((t0.OHCUST = ?) OR (t0.OHBID IN (?,?))) OR (t0.OHSID IN (?,?,?,?,?,?,?,?,?,?))) OR (t0.OHCID IN (?,?,?,?,?,?,?,?,?,?)))) AND (((t1.CUCID = t0.OHSID) AND (t1.CUCM = t0.OHCM)) AND ((t3.CUCID = t0.OHCID) AND (t3.CUCM = t0.OHCM)))) ORDER BY t0.OHOR# DESC) AS EL_TEMP) AS EL_TEMP2 WHERE EL_ROWNM <= ?) AS EL_TEMP3 WHERE EL_ROWNM > ?

共有1个答案

潘鸿文
2023-03-14

我不知道applyPagination是如何打破顺序的,但这里使用query.limit和query.offset可能更直观,因为您在存储库方法中。

...
.orderBy(order.number.desc()).
.limit(pageSize)
.offset((page-1) * pageSize);
 类似资料:
  • 问题内容: @Query(value =“从owner_null或owner =?1的paper_entry中选择,countQuery =”从owner_null不为null或owner =?1的paper_entry中选择count( ),nativeQuery = true) 页面findAll(Long userId,Pageable pageable); 我使用的是mysql 5.7,s

  • 问题内容: 我的数据库中有三个表。产品表,类型表和映射表Prod_Type。我的数据库是sql server,这就是为什么我不能使用group_concat函数而使用Stuff函数的原因。我的表结构如下 产品表 类型表 Prod_TypeTable 我能够将产品表加入到Prod_type的映射表中。我使用了东西查询来避免出现多个结果。我的查询是这样的: 我现在需要做的是将我先前的查询结果连接到类型

  • QueryDSL定义了一个接口,通过调用或可以轻松地为任何字段获取该接口的实例。Spring Data JPA的接口甚至有一个方法,它将作为参数。 但是对QueryDSL一无所知,它有自己定义查询排序顺序的方法,即。它可以包含许多,它们与非常相似,只是它们不是类型安全的。 所以,如果我想做使用排序的分页查询,真的没有办法使用QueryDSL来定义它吗?

  • SpringDataJpa中 Specification怎样使用in查询 实体类Menu.java如下 想通过roles属性做in查询,代码如下 错误如下: Parameter value [com.appmtce.pojo.entity.role.Role@20f81e2a] did not match expected type [java.util.Collection (n/a)] 我的S

  • 您可以看到,子查询中没有使用协议实体路径a1的别名,而是被协议实体路径A2的别名所取代。在querydsl中还需要做一些其他的事情才能生成这个查询吗?

  • 问题内容: 我已经看到了许多有关在Sql查询中使用参数和“赞”的问题,但是我尝试了各种方法来对其进行编码,但仍然无法使我的查询给出结果。如果我在查询本身中输入一个值,它会很好地运行。当我运行列出的第一个查询时,出现错误“必须声明标量变量“ @Search”,但我认为我使用cmd.Parameters.AddWithValue语句做到了。有人可以看到我可能在做错什么吗?感谢您的任何帮助。 问题答案: