Hibernate / Order By / Rownum /错误的分页结果返回

时间:2018-03-14 15:02:31

标签: sql oracle hibernate criteria

我正在使用带有order by的条件查询,用于页面大小为10的分页。 例如,对于查询的" getResultList()" 20行: 第一页显示10个结果(" setFirstResult(0),设置MaxResults(10)" - >用于分页)。

第二页显示10个结果(setFirstResult(10),设置MaxResults(10) - >用于paginatio)

    The problem is that the second page contains also some results from the first page.
    After checking the generated SQL I noticed that the order by is missing from the outer query:

//Criteria Code  
Query q=..              
          query.setFirstResult(paginate.getFirst()).setMaxResults(paginate.getRows());
result = query.getResultList();
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>

首页生成SQL:

      select
               *
            from
                ( select
                    usernotifi0_.USER_NOTIFICATION_CASE_ID as USER_NOTIFICATION_1_47_,
                    usernotifi0_.ARCHIVED as ARCHIVED2_47_,
                    usernotifi0_.NOTIFICATION_CASE_ID as NOTIFICATION_CASE_4_47_,
                    usernotifi0_.READ as READ3_47_,
                    usernotifi0_.USER_INFORMATION_ID as USER_INFORMATION_I5_47_ 
                from
                    USER_NOTIFICATION_CASE usernotifi0_ 
                inner join
                    NOTIFICATION_CASE notificati1_ 
                        on usernotifi0_.NOTIFICATION_CASE_ID=notificati1_.NOTIFICATION_CASE_ID 
                inner join
                    COUNTRY country3_ 
                        on notificati1_.MAIN_MEMBER_STATE=country3_.COUNTRY_KEY 
                where
                    usernotifi0_.USER_INFORMATION_ID=161 
                    and usernotifi0_.ARCHIVED=0
                    and notificati1_.NOTIFICATION_STATUS<>'TRASH'
                order by
                    country3_.COUNTRY_KEY desc ) 
            where
                rownum <= 10 

        Second page generated SQL:
           select     *
            from
                ( select
                    row_.*,
                    rownum rownum_ 
                from
                    ( select
                        usernotifi0_.USER_NOTIFICATION_CASE_ID as USER_NOTIFICATION_1_47_,
                        usernotifi0_.ARCHIVED as ARCHIVED2_47_,
                        usernotifi0_.NOTIFICATION_CASE_ID as NOTIFICATION_CASE_4_47_,
                        usernotifi0_.READ as READ3_47_,
                        usernotifi0_.USER_INFORMATION_ID as USER_INFORMATION_I5_47_ 
                    from
                        USER_NOTIFICATION_CASE usernotifi0_ 
                    inner join
                        NOTIFICATION_CASE notificati1_ 
                            on usernotifi0_.NOTIFICATION_CASE_ID=notificati1_.NOTIFICATION_CASE_ID 
                    inner join
                        COUNTRY country3_ 
                            on notificati1_.MAIN_MEMBER_STATE=country3_.COUNTRY_KEY 
                    where
                        usernotifi0_.USER_INFORMATION_ID=161 
                        and usernotifi0_.ARCHIVED=0
                        and notificati1_.NOTIFICATION_STATUS<>'TRASH'
                    order by
                        country3_.COUNTRY_KEY desc ) row_ 
                where
                    rownum <= 20
                ) 
            where

rownum_&gt; 10

对于page1:我修改了查询以返回唯一的notification_case_id和页面大小为20的rownum:

select
        NOTIFICATION_CASE_4_47_,rownum
    from
        ( select
            usernotifi0_.USER_NOTIFICATION_CASE_ID as USER_NOTIFICATION_1_47_,
            usernotifi0_.ARCHIVED as ARCHIVED2_47_,
            usernotifi0_.NOTIFICATION_CASE_ID as NOTIFICATION_CASE_4_47_,
            usernotifi0_.READ as READ3_47_,
            usernotifi0_.USER_INFORMATION_ID as USER_INFORMATION_I5_47_ 
        from
            USER_NOTIFICATION_CASE usernotifi0_ 
        inner join
            NOTIFICATION_CASE notificati1_ 
                on usernotifi0_.NOTIFICATION_CASE_ID=notificati1_.NOTIFICATION_CASE_ID 
        where
            usernotifi0_.USER_INFORMATION_ID=41 
            and usernotifi0_.ARCHIVED=0
            and notificati1_.NOTIFICATION_STATUS<>'TRASH'
        order by
            notificati1_.MAIN_MEMBER_STATE desc ) 
    where
        rownum <= 20

结果是(case_id rownum):

(764 1) (681 2) (704 3) (765 4) (284 5)
(601    6)
(683    7)
(241    8)
(268    9)
(270    10)
(467    11)
(514    12)
(541    13)
(586    14)
(276    15)
(367    16)
(368    17)
(515    18)
(459    19)
(584    20)

对于page1,页面大小为10的结果为:

(764    1)
(681    2)
(704    3)
(765    4)
(284    5)
(241    6)
(268    7)
(270    8)
(467    9)
(514    10)

两种情况下返回的结果对于前10行应该是相同的,但它们不是。对于rownum 6,结果开始有所不同。

0 个答案:

没有答案