public List<T> findRange(int start, int duration) {
Session hibernateSession = this.getSession();
List<T> results = null;
try {
hibernateSession.beginTransaction();
Query q = hibernateSession.createQuery("From " + type.getSimpleName() );
q.setFirstResult(start);
q.setMaxResults(duration);
results = q.list();
hibernateSession.getTransaction().commit();
} finally {
hibernateSession.close();
}
return results;
}
我有上面的查询从我的数据库获取某个行范围,我需要检索它们在jobStatus列上排序,而不是按字母顺序或数字顺序但具有以下重要性:
case "Open":
return 1;
break;
case "On Hold":
return 2;
break;
case "Offer Extended":
return 3;
break;
case "Closed":
return 4;
break;
如何重写这个hibernate查询,以便如果我想要第10行到第20行,我会按顺序从表中检索它们?
答案 0 :(得分:0)
一般来说,我认为你不会侥幸逃脱;
Query q = hibernateSession.createQuery("FROM " + type.getSimpleName() +
" ORDER BY CASE WHEN jobStatus='Open' THEN 1 " +
"WHEN jobStatus='On Hold' THEN 2 " +
"WHEN jobStatus='Offer Extended' THEN 3 " +
"WHEN jobStatus='Closed' THEN 4 "
"ELSE 5 END, id");