|
http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html contains a section titled 'Pagination with ROWNUM' where an example is given on some possible scenarios that can occur when using rownum in oracle.
Specifically looking at "One important thing about using this pagination query is that the ORDER BY statement should order by something unique. If what you are ordering by is not unique, you should add something to the end of the ORDER BY to make it so. If you sort 100 records by SALARY, for example, and they all have the same SALARY value, then specifying rows 20 to 25 does not really have any meaning. In order to see this, use a small table with lots of duplicated ID values...."
Example oracle sql from link using oracle rowid:
create table t as select mod(level,5) id, trunc(dbms_random.value(1,100)) data from dual connect by level <= 10000;
select * from (select a.*, rownum rnum from (select id, data from t order by id) a where rownum <= 150) where rnum >= 148;
select * from (select a.*, rownum rnum from (select id, data from t order by id) a where rownum <= 151) where rnum >= 148;
select * from (select a.*, rownum rnum from (select id, data from t order by id, rowid) a where rownum <= 150) where rnum >= 148;
select * from (select a.*, rownum rnum from (select id, data from t order by id, rowid) a where rownum <= 151) where rnum >= 148;
I have attached an example test project which is based on example in link and uses hibernate. Test runs similar sql and shows the differences when rowid is appended at the end of order by.
Can always add another order by that is on a unique column like link states, but was wondering if this might be something to always add to sql with order by when using oracle and hibernate setMaxResults/setFirstResult.
|