Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPQL for select from select row_number() over()

Tags:

jpa

jpql

I'm using Db2 on AS/400, and I am trying to execute a JPQL query that will return results from row x to row y.

In SQL this works:

select cur.* from (
  SELECT ROW_NUMBER() OVER() AS ROWNUM FROM tableName d) as cur
WHERE cur.ROWNUM > 0 AND cur.ROWNUM < 10

How can I do this in JQPL? I tried it in many ways but every time I got an exception.

I want to limit my result inside the query, and not by using the setMaxResult, setFirstResult methods.

like image 935
user590586 Avatar asked Jun 26 '12 11:06

user590586


2 Answers

Query q = em.createQuery("select e from SomeEntity e")
            .setFirstResult(0)
            .setMaxResults(10);
like image 63
JB Nizet Avatar answered Nov 03 '22 01:11

JB Nizet


That cannot be done. JPQL operates to entities and entities are mapped to tables in database. Row number in db2 is concept in result set, not in database table.

like image 32
Mikko Maunu Avatar answered Nov 03 '22 01:11

Mikko Maunu