-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Paging Native SQL Query in JPA with Hibernate without error
PostPosted: Mon Jan 30, 2012 7:45 pm 
Newbie

Joined: Mon Jan 30, 2012 7:38 pm
Posts: 1
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:

Code:
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:
Code:
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!! :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.