【发布时间】:2017-03-06 18:39:52
【问题描述】:
SELECT * FROM orderTable order by
CASE priority when 'CRITICAL' THEN 1
when 'HIGH' then 2
when 'MEDIUM' then 3
when 'LOW' then 4
when 'NOT_ASSIGNED' then 5
end ASC ,
CreatedAt ASC;
这是我的 mysql 查询及其工作。数据库列priority是字符串类型,该字符串具有以下优先级
我想要像 jpa 这样的语言
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(entityClass);
Root<T> root = cq.from(entityClass);
cb.selectCase().when(cb.equal(root.get("priority"), "CRITICAL"), 1)
.when(cb.equal(root.get("priority"), "HIGH"), 2)
.when(cb.equal(root.get("priority"), "MEDIUM"), 3)
.when(cb.equal(root.get("priority"), "LOW"), 4)
.when(cb.equal(root.get("priority"), "NOT_ASSIGNED"), 5).;
Order temp2 = cb.desc(root.get("priority"));
cq = cq.orderBy(temp2);
这是行不通的,它返回结果仅按字符串优先级排序。选择案例不适用于标准构建查询。
【问题讨论】: