0
    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;

我怎样才能重新处理这个休眠查询,以便如果我想要第 10 行到第 20 行,我可以按这个顺序从表中检索它们?

4

1 回答 1

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");
于 2013-07-23T20:42:40.127 回答