I got problem in coding JPA query against my MySQL table.
The table structure is shown as below:
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)
The following SQL got executed as expected.
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
The payment can be ordered correctly.
In my spring-boot JPA repository interface: @Transactional @Repository public interface ProductPurchaseMeasurementRepository extends 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);
}
The parameter orderBy will be passed with value "payment". But I can't get ordered records in the returned value of the method.
Anybody got idea why the repository method is not working as expected? Thanks.