All children or only one child is loaded when specify lazy loading in JPA?
I coudn't find the behavior of lazy loading, If I have a "one to many" relationship in JPA annotated as lazy loading, when in my code I call the method "getChildren()" from parent what happen?:
a) EclipseLink load all the children of that parent
b) EclipseLink load one child at the time of that parentIf the response is "a" does it mean that for large data sets like thousands or millions of records I should not use the "one to many" model and implement my own server side pagination model?
开发者_如何学编程thank you.
A) - when you access a OneToMany all of the children will be loaded.
If you OneToMany is too big to load, then do not map it. Query for it instead. You can set the firstResult/maxResults on the Query.
A @BatchSize would not help on a OneToMany.
In JPA proper,there is currently noway to specify the batch size. So really the answer to your question is (c), it is up to the implementation to define what that is for your OneToMany collection. Most likely it will be a number bigger than one, but it is not specified.
If you are using Hibernate, there is the ability to use the @BatchSize annotation to specify how many to load.
If you are doing pagination, one of the issues that you need to deal with is the starting point for your collection. So if you are displaying 50 items per page and your user is on page 13 of 50, you want to display items 601 - 650 from your collection. Even with JPA lazy loading, there is no way for you to give it a position in the collection that you want. So you probably will want your own pagination scheme using a Query with a specified start position and max results.
精彩评论