Suppose I have a Post entity and a Comment entity and a one to many relationship:
@Entity class Post {
...
@OneToMany
List comments;
Is it possible to emulate dynamic paging with @OneToMany collections on top of JPA (...)
Not supported. The standard approach would be to use a JPQL query to retrieve the comments for a given post and and to use Query#setFirstResult(int)
and Query#setMaxResults(int)
.
On my first thought, we could create a subclass of List, (...). But I don't know whether Hibernate/JPA will recognize this, or interfere with it.
It obviously won't without an heavy patch to drastically change the default behavior.
I think the "right" way might be more like:
@Entity
class Post {
...
public GenericModel.JPAQuery getComments() {
return Comment.find("post_id = ?", post_id);
}
}
and then use one of the fetch
methods in JPAQuery
:
// fetch first page of results, 25 results per page
post.getComments().fetch(1,25);