Hibernate Select Top and Bottom n Rows with Criteria

前端 未结 7 2172
孤街浪徒
孤街浪徒 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:59

    I don't think you can do what you want using a single query. You can certainly do it in two, however (using EJBQL with named parameters):

    SELECT r FROM Reviews r WHERE r.book = :book ORDER BY r.stars ASC LIMIT 3;
    SELECT r FROM Reviews r WHERE r.book = :book ORDER BY r.stars DESC LIMIT 3;
    

    And you could of course write a simple helper method that makes both of these calls and collates the results into whatever data structure you prefer.

提交回复
热议问题