Hibernate Select Top and Bottom n Rows with Criteria

前端 未结 7 1985
孤街浪徒
孤街浪徒 2021-02-07 00:33

Let\'s say I have two tables, Books and Reviews. Reviews has a column, stars, that can have a value between 1 and 5. A Book can have many Reviews.

How would I select al

7条回答
  •  花落未央
    2021-02-07 00:55

    Try this and let me know if it works for you:

      // you should have the DAO class containing this queries 
      // extend HibernateDaoSupport
    
      List topList = getSession().createQuery("FROM Reviews r WHERE r.book = " 
              + book + " ORDER BY r.stars asc").setMaxResults(3).list();
    
      List bottomList = getSession().createQuery("FROM Reviews r WHERE r.book = " 
              + book + " ORDER BY r.stars desc").setMaxResults(3).list();
    

提交回复
热议问题