javahibernatejpawindow-functionscriteria-api

How do I use row_number (or anything with same behavior) with Hibernate Criteria API?


I want to implement a similar query with CriteriaBuilder API:

SELECT *, ROW_NUMBER() OVER( ORDER BY Student_Score) AS RowNumberRank
FROM StudentScore

The main problem is that JPQL doesn't support window functions and I don't know how to replicate their behavior...


Solution

  • Analytical functions are vendor specific. This makes is hard to implement with a CriteriaBuilder API. Using native query ties your java application to a specific database vendor. To avoid that you can create a vendor specific view and create in your java application a select with the criteria api using that view.

    Beside the StudentScore entity you already have you had to define a (readonly) StudentScoreView entity with the extra attribute rowNumberRand which you can use in your JPQL query.