I have a JPQL query that retrieves the sorted list of entities from the database. I have the id of one of these entities. How can I get the previous and next record to the record with the known id in my select?
select distinct n from News n
inner join n.author a
inner join n.tags t
where a.id = :authorId and t.id in :tagsId
order by size(n.comments) desc
News has one author, many tags and comments. I select news with the given author and tags and order them by the count of the comments to them.
While using JDBC I've solved such problems using rownum
.
Can I get the position(rownum) of a record in a result using JPA? If I knew the record's position I could define the first result to that position - 1, and max results to 3 for the given query. That is how I could get the previous, current and next news.
Are there any other solutions, except iterating through the list of news?