I have below SQL query:
SELECT * FROM (
SELECT S.aCol, S.bCol, S.cCol, ROW_NUMBER() OVER (ORDER BY S.cCol ASC) AS R
FROM myTable S WHERE S.cCol LIKE 'abc%') AS TEMP
WHERE R BETWEEN 0 AND 10 FOR FETCH ONLY
Basically this query serves pagination. The between clause will have different values for every fetch.
Now I want to implement the same query using JPA.
I found the below reference article from one SO question:
But in this article the resultant list is directly passed over to view. However, in my case I want to set the result columns in a transfer object (mySearchTO) as attributes.
Something like
mySearchTO.setBCol(resultSet.getString("B_COL"));
mySearchTO.setACol(resultSet.getString("A_COL"));
in sql.
How to achieve this with JPA ?
Also, how can I write main query in JPA ?
I can not use Criteria Builder as I have to run my app on WSAD 6.1
The query which I am running is:
select s.aCol, s.bCol, s.cCol from MyEntity s where s.cCol like 'abc' order by s.cCol asc
Above query is returning a List.
How to map it with my TO ?? I am trying
List<mySearchTO> myResult = new ArrayList<mySearchTO>();
myResult = myQuery.getResultList();
Where my TO class is a simple class with 3 properties (aCol, bCol and cCol) and getters and setters.
Error I am getting is
Exception thrown : java.lang.ClassCastException: [Ljava.lang.Object; incompatible with com.SummaryTO
But this is giving
JPA has pagination support with the
Query.setFirstResultandQuery.setMaxResultsmethods. I don’t know how it translates in SQL with DB2, though, but it’s done in database (using rownum with Oracle, limit with MySQL, etc.)Just write your JPA query as you would do without pagination, and call these two methods, and Hibernate will generate the appropriate SQL query: