【问题标题】:Spring-Data-JPA pagingSpring-Data-JPA 分页
【发布时间】:2013-09-15 12:13:51
【问题描述】:

我正在使用Datatables jQuery 插件来显示订单位置列表。 在调试我的应用程序时,我偶然发现了以下不一致。

为了检索数据,我使用@Query-Notation 进行自定义查询:

@Query("select op from OrderPosition  " +
        "op where op.order.created >= ?1" +
        " and op.depot in ?2" +
        " and op.order.deliveryAddress.deliveryMode in ?3" +
        " and op.pickStatus in ?4 and op.order.id like ?5 ")
Page<OrderPosition> findOrderpositionsByFilterStatus(Date date, List<Integer>depots, List<DeliveryMode> deliveryModes, List<ArticleStatusType> pickStatus, String sSearch, Pageable p);

发生的错误如下:

我有一组 81 个 Orderpositions 测试数据。在前端,我可以按几个标准进行过滤。标准之一是deliveryMode(表达|标准)。我一次显示 10 个条目。 expressdeliveries 的总数为 6。当翻阅页面时,仅显示 2 个express。在express 上显式过滤时,我得到了全部 6 个。

当我添加某种排序时,例如:

@Query("select op from OrderPosition  " +
        "op where op.order.created >= ?1" +
        " and op.depot in ?2" +
        " and op.order.deliveryAddress.deliveryMode in ?3" +
        " and op.pickStatus in ?4 and op.order.id like ?5 " +
        "order by op.order.id desc")
Page<OrderPosition> findOrderpositionsByFilterStatus(Date date, List<Integer>depots, List<DeliveryMode> deliveryModes, List<ArticleStatusType> pickStatus, String sSearch, Pageable p);

我得到了所有 6 个。

是什么让我想到了这个问题:

当使用非注释查询时——无论是通用的findAll() 还是来自方法名称的查询——Spring-Data-JPA 在检测到pageable 时是否在内部使用order by 子句?或相反:使用pageable 时,我是否必须在每个自定义查询中添加order by 子句?

【问题讨论】:

  • 在问题中包含数据表真的相关吗?即使您因此发现了这个错误,对我来说这似乎是一个 JPA/Spring-Data-JPA 问题。
  • 可以开启log查看生成的sql并进行验证。
  • 您可以编辑并发布您的解决方案吗?您的查询和主页是什么样的?
  • @Blejzer 我会的。但不幸的是,我不再在公司工作 - 我无法访问代码。
  • 该死!我以为你会是我的救命恩人!!! :D

标签: java spring jpql spring-data-jpa


【解决方案1】:

是的。如果您没有进一步指定,Spring Data 使用 OrderBy(带有默认 desc)。 查看日志:

SELECT t0.oid, t0.jpaversion, t0.amount, [...]
FROM [...]
WHERE [..] AND (t13.jpatype IS NULL OR t13.jpatype IN (?))
ORDER BY t0.oid DESC LIMIT ?, ?

【讨论】:

    猜你喜欢
    • 2021-02-24
    • 2016-02-01
    • 2017-04-04
    • 2012-05-25
    • 2016-11-10
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    相关资源
    最近更新 更多