Hibernate Criteria API: get n random rows

前端 未结 5 1811
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 11:06

I can\'t figure out how to fetch n random rows from a criteria instance:

Criteria criteria = session.createCriteria(Table.class);
criteria.add(Restrictions.e         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 11:36

    You can not fetch random rows efficiently, sorry. Hibernate can only do what SQL does, and random row fetch simply is not part of any standard SQL implementation I know - actually it is to my knowledge not part of ANY SQL that I am aware of (anyone please enlight me).

    And as Hibernate is an O/R mapper, and not a wonder machine, it can only do what the underlying database supports.

    If you have a known filed with ascending numbers and know start and end, you can generate a random number on the computer and ask for that row.

提交回复
热议问题