休眠/排序方式/行号/错误的分页结果被删除

问题描述 投票:0回答:1

我正在使用标准查询,并按order by进行分页,页面大小为10。例如,对于20行的“查询的getResultList()”,则:第一页显示10个结果(“ setFirstResult(0),set MaxResults(10)”->用于分页)。

第二页显示10个结果(setFirstResult(10),set MaxResults(10)->用于分页)

    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_> 10

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

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行应该是相同的,但事实并非如此。对于第6行,结果开始有所不同。

sql oracle hibernate criteria
1个回答
0
投票

,我也有这个问题,您找到解决方法了吗?

请帮助我。

© www.soinside.com 2019 - 2024. All rights reserved.