Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

how can i do paging with onetomany collections

how can i do paging with onetomany collections  using -'java,hibernate,jpa,playframework,siena'

Suppose I have a Post entity and a Comment entity and a one to many relationship:

@Entity class Post {
    List<Comment> comments;

How can I achieve paging like this:

Post post = //Find the post.
return post.getComments().fetch(100, 10); // Find the 11th page (page size 10);

Is it possible to emulate dynamic paging with @OneToMany collections on top of JPA,
or do we have to rewrite the association mechanism of JPA totally ? (e.g. create a PersistentList collection type that could manage the paging, sorting and searching).

P.S.: I recently found the Play! framework uses a very interesting lib on top of JPA: Siena. Siena is very easy to use, and is a good abstraction on top of JPA/Hibernate. But I can't find how to do paging with its associations.


Play framework has a query syntax similar to Django:

Post.findAll().from(100).fetch(10);  // paging



will return a JPAQuery object, a customized query type in Play.

But with associated collections, e.g.:


will just return a List, which doesn't support paging or other queries.

I was wondering how to extend it, so that


will also return a JPAQuery object or similar, then you can query on the "query" collection:


or insert a new Comment without actually fetching any of the comments:

Post.comments.add(new Comment(...));

On my first thought, we could create a subclass of List, then the Post class would become:

@Entity class Post {
    QueryList<Comment> comments;

and QueryList will have fetch(), from() methods that indirect to JPAQuery's.

But I don't know whether Hibernate/JPA will recognize this, or interfere with it.

asked Oct 11, 2015 by badhwar.rohit
0 votes

Related Hot Questions

Government Jobs Opening