分类的分页不使用Hibernate在Oracle数据库中工作

发布于 2025-02-13 16:10:23 字数 1185 浏览 0 评论 0原文

我正在使用Hibernate的Spring JPA。当我将分页用于排序(通常在表中)时,Oracle10Gdialect

select row_.*, rownum rownum_ from ( 
select table_.tablefield1, table_.tablefield2, table_.tablefield3... 
from table table_ where <condition>
order by table_tablefield1 desc 
) row_ where rownum <= 5

根据 this 解释,在这种情况下,命令不被视为Rownum更改子查询的顺序。 实际上,我正在遇到这个问题。如果我不放置任何分类字段,一切都很好。

我打开了a bug 在Hibernate Orm中,但自6个月以来没有反馈。有人可以帮忙吗?

环境 Spring Boot 2.2.0,Java 8,Linux,Oracle 19.0.0.0.0

备注! 这个问题不重复 this 一个,因为我无法更改Hibernate生成的SQL。在标记重复之前,请检查标签。

I'm using spring jpa with hibernate. When I use the pagination with sorting (typically in tables) the Oracle10gDialect generates the following SQL

select row_.*, rownum rownum_ from ( 
select table_.tablefield1, table_.tablefield2, table_.tablefield3... 
from table table_ where <condition>
order by table_tablefield1 desc 
) row_ where rownum <= 5

According to this explanation, the order by is in that case not considered as the rownum changes the order of the subquery.
And in fact I'm experiencing the issue. Everything works well if I don't put any sorting field.

I opened a bug in Hibernate ORM but no feedback since more than 6 months. Anybody can help?

Environment
Spring boot 2.2.0, Java 8, Linux, Oracle 19.0.0.0.0

REMARK!
This question does not duplicate this one because I can't change the SQL generated by hibernate. Please check the tags before marking as duplicate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪意 2025-02-20 16:10:24

我确实相信,HBN生成的SQL首先和下一个页面都是正确的。您实际上很可能面对排序稳定性问题:如果某些记录具有您要订购的列的相同值,我们对他们的顺序无话可说,Oracle也不能说。 ,要使排序稳定,您需要添加一些独特的东西(即id)以按子句订购。

select row_.*, rownum rownum_ from ( 
select table_.tablefield1, table_.tablefield2, table_.tablefield3... 
from table table_ where <condition>
order by table_.tablefield1 desc, table_.id 
) row_ where rownum <= 5

您可能不会在其他DBS中遇到类似问题,因为其他DBS可能会使用聚类索引来支持PK。

是的,“现代语法”无法解决排序稳定性问题。

I do believe the SQLs generated by HBN to get both first and next pages are correct. It is more likely you are actually facing with sort stability issue: if some records has the same value of column you are ordering by, we can say nothing about their order, and oracle can't say as well, to make sort stable you need to add something unique (i.e. id) to order by clause.

select row_.*, rownum rownum_ from ( 
select table_.tablefield1, table_.tablefield2, table_.tablefield3... 
from table table_ where <condition>
order by table_.tablefield1 desc, table_.id 
) row_ where rownum <= 5

You might not facing with similar issue in other DBs because other DBs might use clustered index to support PK.

And yes, "modern syntax" won't solve sort stability issue.

思念绕指尖 2025-02-20 16:10:24

上面的语法没有错。它可以正常工作,

SQL> select * from emp;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500                    30
      7876 ADAMS      CLERK           7788 12-JAN-83       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected.

SQL>
SQL> select * from emp
  2  order by hiredate desc;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7876 ADAMS      CLERK           7788 12-JAN-83       1100                    20
      7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

14 rows selected.

SQL>
SQL> select row_.*, rownum rownum_ from (
  2    select * from emp
  3    order by hiredate desc
  4  ) row_ where rownum <= 5;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO    ROWNUM_
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
      7876 ADAMS      CLERK           7788 12-JAN-83       1100                    20          1
      7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20          2
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10          3
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30          4
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20          5

5 rows selected.

也许您指的是获取后续页面(2,3,4 ...) - 如果是这样,我们需要查看为此生成的查询。

There is nothing wrong with the syntax above. It works correctly

SQL> select * from emp;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500                    30
      7876 ADAMS      CLERK           7788 12-JAN-83       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected.

SQL>
SQL> select * from emp
  2  order by hiredate desc;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7876 ADAMS      CLERK           7788 12-JAN-83       1100                    20
      7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

14 rows selected.

SQL>
SQL> select row_.*, rownum rownum_ from (
  2    select * from emp
  3    order by hiredate desc
  4  ) row_ where rownum <= 5;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO    ROWNUM_
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
      7876 ADAMS      CLERK           7788 12-JAN-83       1100                    20          1
      7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20          2
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10          3
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30          4
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20          5

5 rows selected.

Perhaps you are referring to getting subsequent pages (2,3,4...) - if that is the case, we'd need to see the query generated for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文