使用 Hibernate 在 JPA 中分页本机 SQL 查询没有错误

发布于 2024-12-29 15:39:12 字数 2905 浏览 0 评论 0 原文

感谢您的关注,并对 mi English 表示歉意:S

我正在使用 JPA 2.0 和 Hibernate 4.X 来执行一些 sql 本机查询。代码非常简单:

private void doIt() throws Exception {
        EntityManager em = getEntityManager();
        Query q = em.createNativeQuery("A very simple native query, which return no entities, but collections of arrays");
        q.setFirstResult(0);
        q.setMaxResults(5);
        Collection<Object> results = q.getResultList();
        System.out.println("1"); //Means page 1
        for (Object elem : results) {
            String line = "";
            Object[] row = (Object[]) elem;
            for (Object object : row) {
                if(object==null){
                    object="null";
                }
                line += object +"("+object.getClass()+")"+ ",";
            }
            System.out.println(row.length + " " + line);

        }


        em = getEntityManager();
        q = em.createNativeQuery("The same simple native query, which return no entities, but collections of arrays");
        q.setFirstResult(5);
        q.setMaxResults(5);
        results = q.getResultList();
        System.out.println("2"); //Means page 2
       for (Object elem : results) {
            String line = "";
            Object[] row = (Object[]) elem;
            for (Object object : row) {
                if(object==null){
                    object="null";
                }
                line += object +"("+object.getClass()+")"+ ",";
            }
            System.out.println(row.length + " " + line);

        }


        em = getEntityManager();
        q = em.createNativeQuery("The same simple native query, which return no entities, but collections of arrays");
        q.setFirstResult(10);
        q.setMaxResults(5);
        results = q.getResultList();
        System.out.println("3"); //Means page 3
        for (Object elem : results) {
            String line = "";
            Object[] row = (Object[]) elem;
            for (Object object : row) {
                if(object==null){
                    object="null";
                }
                line += object +"("+object.getClass()+")"+ ",";
            }
            System.out.println(row.length + " " + line);

        }
    }

我的结果是这样的:

1 
data1,data2,...,data-n        -->I need this output
data1,data2,...,data-n
data1,data2,...,data-n
data1,data2,...,data-n
data1,data2,...,data-n

2
data1,data2,...,data-n,6      -->OMG! lol
data1,data2,...,data-n,7
data1,data2,...,data-n,8
data1,data2,...,data-n,9
data1,data2,...,data-n,10

3
data1,data2,...,data-n,11
data1,data2,...,data-n,12
data1,data2,...,data-n,13
data1,data2,...,data-n,14
data1,data2,...,data-n,15

简而言之,第一页的输出每行有 n 项(这是我想要的输出),但是第二页和第三页有 n+1 项,并且附加项似乎是已带来的行号。

有人遇到同样的事情吗?我在 Hibernate 的文档中进行了搜索,但无法解决这个“有趣的:@”问题。

这段代码是用Toplink执行的,没有问题。

非常感谢!! :)

Thanks for your attention, and sorry for mi English :S

I'm using JPA 2.0 with Hibernate 4.X to do some sql native queries. Te code is very simple:

private void doIt() throws Exception {
        EntityManager em = getEntityManager();
        Query q = em.createNativeQuery("A very simple native query, which return no entities, but collections of arrays");
        q.setFirstResult(0);
        q.setMaxResults(5);
        Collection<Object> results = q.getResultList();
        System.out.println("1"); //Means page 1
        for (Object elem : results) {
            String line = "";
            Object[] row = (Object[]) elem;
            for (Object object : row) {
                if(object==null){
                    object="null";
                }
                line += object +"("+object.getClass()+")"+ ",";
            }
            System.out.println(row.length + " " + line);

        }


        em = getEntityManager();
        q = em.createNativeQuery("The same simple native query, which return no entities, but collections of arrays");
        q.setFirstResult(5);
        q.setMaxResults(5);
        results = q.getResultList();
        System.out.println("2"); //Means page 2
       for (Object elem : results) {
            String line = "";
            Object[] row = (Object[]) elem;
            for (Object object : row) {
                if(object==null){
                    object="null";
                }
                line += object +"("+object.getClass()+")"+ ",";
            }
            System.out.println(row.length + " " + line);

        }


        em = getEntityManager();
        q = em.createNativeQuery("The same simple native query, which return no entities, but collections of arrays");
        q.setFirstResult(10);
        q.setMaxResults(5);
        results = q.getResultList();
        System.out.println("3"); //Means page 3
        for (Object elem : results) {
            String line = "";
            Object[] row = (Object[]) elem;
            for (Object object : row) {
                if(object==null){
                    object="null";
                }
                line += object +"("+object.getClass()+")"+ ",";
            }
            System.out.println(row.length + " " + line);

        }
    }

And my result is this:

1 
data1,data2,...,data-n        -->I need this output
data1,data2,...,data-n
data1,data2,...,data-n
data1,data2,...,data-n
data1,data2,...,data-n

2
data1,data2,...,data-n,6      -->OMG! lol
data1,data2,...,data-n,7
data1,data2,...,data-n,8
data1,data2,...,data-n,9
data1,data2,...,data-n,10

3
data1,data2,...,data-n,11
data1,data2,...,data-n,12
data1,data2,...,data-n,13
data1,data2,...,data-n,14
data1,data2,...,data-n,15

In short, the output in the first page has n items per row (that's my desired output), but the second and third pages have n+1 items, and the additional item seems to be the number of the row that has been brought.

Someone has the same thing happened? I have searched in the Hibernate's documentation, but I could not solve this "funny :@" problem.

This code was executed with Toplink, and It doesn' have the problem.

Thank you very much!! :)

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

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

发布评论

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

评论(1

櫻之舞 2025-01-05 15:39:13

这就是 Hibernate 中完成分页的方式——使用其 Dialect 实现。 Dialect 的 API 如下:它接受要分页的 SQL 查询和范围信息,并生成产生所需行范围的 SQL 语句。

在 Oracle Dialect 中,它可以使用 ROWNUM 并且不需要修改您的原始查询。例如 DB2 或 SQL Server 方言需要显着改变您的查询(包括但不限于在输出中添加附加列),以便能够过滤所需的范围 你。

最后一件事——为什么你的首页或结果不同。 SQL 允许(取决于实现)像 SELECT TOP n 这样的构造,因此查询范围 [0, n][m, n] > 通常是不同的。

This is the way paging is done in Hibernate -- using its Dialect implementations. The API for a Dialect is as follows: it takes SQL query to be paged and range information, and produces SQL statement yielding desired rows range.

In case or Oracle Dialect, it can use ROWNUM and needs not modify your original query. In case of e.g. DB2 or SQL Server a dialect needs to significantly alter your query (incl. but not limited to adding additional column in the output) to be able to filter desired range for you.

And final thing -- why your first page or result is different. SQL allows (depending on implementation) for constructs like SELECT TOP n, so the query for range [0, n] and [m, n] is often different.

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