Pregunta

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

¿Fue útil?

Solució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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top