【问题标题】:How to make SQL query by ordering aggregated column in spring-boot with JPA?如何通过使用 JPA 在 spring-boot 中排序聚合列来进行 SQL 查询?
【发布时间】:2015-11-02 13:04:08
【问题描述】:

我在针对我的 MySQL 表编写 JPA 查询时遇到问题。

表结构如下:

mysql> desc t_product_purchase;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| oid             | bigint(20)   | NO   | PRI | NULL    |       |
| consumer_id     | bigint(20)   | NO   |     | NULL    |       |
| number          | bigint(20)   | YES  |     | NULL    |       |
| pay_time        | datetime     | YES  |     | NULL    |       |
| payment         | float        | YES  |     | NULL    |       |
| price           | float        | YES  |     | NULL    |       |
| product_num_iid | bigint(20)   | NO   |     | NULL    |       |
| seller_id       | bigint(20)   | NO   | MUL | NULL    |       |
| product_title   | varchar(255) | YES  |     | NULL    |       |
+-----------------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

以下 SQL 已按预期执行。

select m.product_num_iid, sum(m.payment) as payment, sum(m.number) as nbr from t_product_purchase m where m.seller_id = 247475251 and m.pay_time >= 0  group by m.product_num_iid order by payment desc limit 0, 10

付款可以正确下单。

在我的 spring-boot JPA 存储库界面中: @Transactional @Repository 公共接口 ProductPurchaseMeasurementRepository 扩展 JpaRepository {

@Query(value = "select m.product_num_iid, sum(m.payment) as payment, sum(m.number) as nbr from t_product_purchase m where m.seller_id = ?1 and m.pay_time >= ?2 and m.pay_time < ?3 group by m.product_num_iid order by ?4 desc limit ?5, ?6", nativeQuery = true)
public List findRankedProductPaymentOrCountBySellerIdWithinPayTime(long sellerId, Date startPayTime, Date endPayTime, String orderBy, long offset, long size);

}

参数 orderBy 将传递值“payment”。但是我无法在方法的返回值中得到有序的记录。

有人知道为什么存储库方法没有按预期工作吗?谢谢。

【问题讨论】:

  • 启用显示sql并检查发送到数据库的查询
  • ORDER BY ?4 中的占位符被解释为文字,而不是列名,这就是它不起作用的原因。有关详细信息,请参阅this post
  • @BohuslavBurghardt - 谢谢。你提供的帖子真的很有帮助。我会试验一下。

标签: java mysql spring jpa


【解决方案1】:

不支持,输入参数只能在 WHERE 和 HAVING 块中使用,不能使用 ORDER BY 子句的参数。

解决方案

order by 
case when 'a' = :a then ec.ISACTIVE else 1  end asc,
case when 'a' != :a then ec.ISACTIVE else 1  end desc

以下解决方案更好:

  • 有尽可能多的命名查询排序顺序
  • 将排序字符串连接到查询字符串
  • 使用条件查询

Hibernate Named Query Order By parameter

【讨论】:

    【解决方案2】:

    您的 ?4 参数是字符串,但框架将其绑定不是字符串值。按语法排序是列名而不是字符串(varchar)!如果要使用动态限制和排序,最好使用 PagingAndSortingRepository。 Spring Data JPA 目前不支持本地查询的动态排序,因为它必须操作声明的实际查询,而对于本地 SQL,它不能可靠地做到这一点。 尝试使用 JPQL 查询。示例:

     @Query("select u from User u where u.lastname like ?1%")
      List<User> findByAndSort(String lastname, Sort sort);
    

    调用这个方法:

    repo.findByAndSort("lannister", new Sort(new Sort.Order(Direction.ASC, "lastName")));     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-12
      • 2019-07-18
      • 2019-04-29
      • 2020-11-13
      • 2019-10-04
      • 1970-01-01
      • 2020-01-13
      • 2023-03-09
      相关资源
      最近更新 更多