我是JPA的新手并拥有以下JPA查询:
String qry = " SELECT e from Event e"
+ " WHERE e.meeting.meetingDate= :pdate"
+ " AND e.meeting.feedId IN (1,2)"
+ " AND e.status <> 'W'"
+ " AND e.meeting.status ='A'"
+ " AND e.settleStatus is NULL"
+ " AND e.offTime is NULL"
+ " AND e.eventCode >'07:45:00'"
+ " group by e.id";
我需要动态添加 ORDER BY 子句。我使用MySQL,请任何人告诉我如何使用 JPA 将以下条件添加到我的查询中。
添加部分:
ORDER BY
CASE WHEN m.country_code ='AU' THEN e.timestamp_updated
WHEN m.country_code <> 'AU' THEN e.event_code
END DESC
如何将此查询细分添加到我的JPA查询中?
答案 0 :(得分:1)
从JPA 2.0起,您可以在JPQL中使用CASE WHEN。
所以你必须做那样的事情:
ORDER BY
CASE
WHEN (e.property = ?) THEN 1
WHEN (e.property = ?) THEN 2
ELSE 3
END DESC
您还可以使用Criteria API
Criteria criteria = session.createCriteria(YourEntity.class);
if(clause){
criteria.addOrder( Order.asc("property_desired_1") );
}else{
criteria.addOrder( Order.asc("property_desired_2") );
}
criteria.list();