EclipseLink分页和Oracle ROWNUM

时间:2015-06-29 11:09:49

标签: java oracle jpa pagination eclipselink

使用带有规范和分页的EclipseLink JPA 2.5.2,我对Oracle ROWNUM的执行方式有疑问。 EclipseLink生成的查询如下:

  SELECT *
  FROM (SELECT a.*,
               ROWNUM rnum
          FROM (SELECT   t0.ID,
                         t0.participantidentity_id,
                         t0.submitteddate
                    FROM delegation t4,
                         meeting t3,
                         meetinggroup t2,
                         participantidentity t1,
                         reimbursement t0,
                         userdata ud
                   WHERE t0.current_id IS NULL
                     AND t1.activeparticipant_id IS NOT NULL
                     AND t2.responsibleservice_id IN (74)
                     AND t1.ID = t0.participantidentity_id
                     AND t4.ID = t1.delegation_id
                     AND t3.ID = t4.meeting_id
                     AND t2.ID = t3.meetinggroup_id
                     AND t1.userdata_id = ud.ID
                ORDER BY t0.submitteddate ASC) a
         WHERE ROWNUM <= 25)
 WHERE rnum > 0

对第一页运行该查询应该返回0到25之间的行,并且对第二页运行相同的查询应该返回25到50之间的行。

问题是第一页的记录也出现在第二页中,我想这是因为提交日期可以包含NULL值,并且在ROWNUM和rnum条件之前没有应用ORDER BY子句或类似的东西,I& #39;我不确定,因为我现在有点不解......

有人可以解释一下原因吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我根据上述情况进行了某种测试 -

CREATE TABLE T3(AA NUMBER(10),BB NUMBER(10),CC NUMBER(10),DD NUMBER(10));
INSERT INTO T3 VALUES (1,2,3,null);
INSERT INTO T3 VALUES (4,5,6,13);
INSERT INTO T3 VALUES (7,8,9,NULL);
INSERT INTO T3 VALUES (10,11,12,14);

然后 -

 SELECT *
FROM
  (SELECT
    a.*,
    rownum rnum
  FROM
    (SELECT aa AS a1,
      bb   AS a2,
      CC          AS A3,
      dd as a4
    FROM T3
    ORDER BY DD ASC) A
  WHERE ROWNUM <= 2
  )
WHERE rnum > 0;

此测试永远不会返回重复项。

请执行以下查询

SELECT   t0.ID,
                             t0.participantidentity_id,
                             t0.submitteddate
                        FROM delegation t4,
                             meeting t3,
                             meetinggroup t2,
                             participantidentity t1,
                             reimbursement t0,
                             userdata ud
                       WHERE t0.current_id IS NULL
                         AND t1.activeparticipant_id IS NOT NULL
                         AND t2.responsibleservice_id IN (74)
                         AND t1.ID = t0.participantidentity_id
                         AND t4.ID = t1.delegation_id
                         AND t3.ID = t4.meeting_id
                         AND t2.ID = t3.meetinggroup_id
                         AND t1.userdata_id = ud.ID
                    ORDER BY t0.submitteddate ASC

并检查它是否有任何重复。

另外,尝试在sql的开头使用不同的关键字,如select distinct t0.ID...