简体   繁体   中英

How to write a Criteria Builder for case statement in MySQL

I have this complicated Query for which I would like to build query using JPA Criteria Builder

select i.* 
from items i
where category = ? 
order by
(price >= ?) desc, 
(case when price >= ? then price end) asc, 
price desc limit 1;

I am not that familiar with Criteria Builder but have basic knowledge.

Thanks in advance.

Unfortunately, JPA does not currently support case expressions in the ORDER BY clause.

If you take a look at the arguments of CriteriaQuery.orderBy , the arguments must be of Order type and not Expression type.

Alternatively, you can execute this as a native SQL query then map the results to an entity through @SqlResultSetMapping .

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