I am trying to determine the best way to page/limit the rows returned when querying the children of a OneToMany relationship while using JPA in Play! Framework.
@Entity
public class User extends Model {
@OneToMany(mappedBy="user", cascade=CascadeType.ALL)
public List<CaseFolder> caseFolders;
}
public class CaseFolder extends Model {
@Required
@ManyToOne
public User user;
@Required
public String number;
public String description;
}
I realize I can set the relationship the lazy fetching. However, that still doesn’t seem to stop me from retrieving the entire list of CaseFolders when I finally access user.caseFolders.
Ideally, I would like to be able to do something like:
user.getCaseFolders().start(100).limit(10)
but I can’t find anything about doing that “out of the box”.
Does everyone really bring the entire related data set into memory every time they need a few of the “children” of a 1-m relationship?
I’m implementing a UI that has paging (using jqgrid) and a user can potentially have thousands of records.
After further research, here’s the answer I came up with:
The “many” side of the relationship – the collection of all caseFolders – is a property of the User. By definition, that property’s value is the entire collection.
To obtain a subset of those elements, I added the following method to my User model: