Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JPA在spring boot中通过对聚合列排序进行SQL查询?_Java_Mysql_Spring_Jpa - Fatal编程技术网

Java 如何使用JPA在spring boot中通过对聚合列排序进行SQL查询?

Java 如何使用JPA在spring boot中通过对聚合列排序进行SQL查询?,java,mysql,spring,jpa,Java,Mysql,Spring,Jpa,我在针对MySQL表编写JPA查询时遇到了问题 表结构如下图所示: mysql> desc t_product_purchase; +-----------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+--------------+------+-----+--

我在针对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存储库界面中: @交易的 @存储库 公共接口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);
@Query(value=“选择m.product\u num\u iid,sum(m.payment)作为付款,sum(m.number)作为来自t\u product\u purchase m的nbr,其中m.seller\u id=?1和m.pay\u time>=?2和m.pay\u time<?3根据m.product\u num\u iid订单按4描述限制5、?6分组,nativeQuery=true)
公共列表FindRankedProductPaymotorCountBySellerIDwithinPayTime(长sellerId、日期开始时间、日期结束支付时间、字符串orderBy、长偏移量、长大小);
}

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


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

不支持,只允许在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
以下解决方案更好:

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

您的?4参数是字符串,但框架将其绑定为字符串值。按语法排序的是列名,而不是字符串(varchar)!如果要使用动态限制和排序,最好使用分页和排序存储库。 SpringDataJPA目前不支持本机查询的动态排序,因为它必须操作所声明的实际查询,这对于本机SQL来说是不可靠的。 尝试使用JPQL查询。示例:

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

启用showsql并检查发送给DBPlaceholder的查询在
orderby?4
中被解释为文字而不是列名,这就是它不起作用的原因。有关详细信息,请参阅。@BohuslavBurghardt-谢谢。你提供的帖子真的很有帮助。我将进行试验。
repo.findByAndSort("lannister", new Sort(new Sort.Order(Direction.ASC, "lastName")));