0

I have an article entity which contains list of comments. What I want is to get the article with comments ordered according to date in desc order and the top 5 comments. I know @OrderBy can be used to set order on association but how to limit the size of association fetched?

ArslanAnjum
  • 1,674
  • 2
  • 17
  • 31
  • Possible duplicate of [Is it possible to limit the size of a @OneToMany collection with Hibernate or JPA Annotations?](https://stackoverflow.com/questions/26328187/is-it-possible-to-limit-the-size-of-a-onetomany-collection-with-hibernate-or-jp) – Jens Schauder Apr 09 '18 at 05:50
  • Also: https://stackoverflow.com/questions/49475975/jpql-hql-limit-items-for-list-of-children https://stackoverflow.com/questions/25073122/how-to-limit-a-collection-in-an-objects-property-in-hibernate – Jens Schauder Apr 09 '18 at 05:51

1 Answers1

0

You need a query, that returns comments.

select c from Article a 
join a.comments c 
where a.id = :articleId 
order by c.date desc 

Then you can call setMaxResults(5) on the query, and execute it.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255