Get record with max id, using Hibernate Criteria

后端 未结 8 1694
后悔当初
后悔当初 2020-12-15 16:18

Using Hibernate\'s Criteria API, I want to select the record within a table with the maximum value for a given column.

I tried to use Projections, creating an alias

8条回答
  •  自闭症患者
    2020-12-15 17:13

    HQL:

    from Person where person.id = (select max(id) from Person)
    

    Untested. Your database needs to understand subselects in the where clause.

    Too lazy to find out if/how such a subselect can be expressed with the criteria api. Of course, you could do two queries: First fetch the max id, then the entity with that id.

提交回复
热议问题