简体   繁体   中英

How to make SQL query by ordering aggregated column in spring-boot with JPA?

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.

Unsupported, input parameters are only allowed in WHERE and HAVING blocks and parameters for the ORDER BY clause can not be used.

solution

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

the following solution is better:

  • Have as much named queries as possible sort orders
  • Concatenate the ordering string to the query string
  • Use criteria queries

Hibernate Named Query Order By parameter

Your ?4 parameter is String, but framework binds it not the String value..Order by syntax is column name not the String (varchar) ! If you want to use dynamic limit and ordering, its best to use PagingAndSortingRepository. Spring Data JPA does not currently support dynamic sorting for native queries, because it would have to manipulate the actual query declared, which it cannot do reliably for native SQL. Try to use JPQL query.Example:

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

Call this method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM