With Spring Data JPA (4.0.8 currently) and Hibernate (4.2.16 currently) I'm having a problem getting pagination to carry down to the subselect hits of JPA OneToMany lists. In the inner list, the subselect selects ALL of the elements to combine with the main, not only the ones for a given page.
For example:
Main JPA entity:
@Entity
@NamedQuery(name="Message.findAll", query="SELECT m FROM Message m")
public class Message implements Serializable {
...
//uni-directional many-to-one association to Message_Review
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinColumn(name="message_id", referencedColumnName="message_id", insertable=false, updatable=false)
@Fetch(FetchMode.SUBSELECT)
private Set<Message_Review> messageReviews;
The SQL being generated to obtain the primary list of JPA entities is as such:
SELECT TOP ? MESSAGE0_.MESSAGE_ID AS MESSAGE_1_0_, MESSAGE0_....... AND NOT (EXISTS (SELECT MESSAGEREV1_.MESSAGE_ID, MESSAGEREV1_.......))
After the base list of JPA entities, Spring Data JPA (or Hibernate) then queries for the OneToMany lists (per the @FetchMode.SUBSELECT). It generates the SQL as such:
SELECT messagerev0_.message_id as message_2_0_1_,
.....
where messagetop0_.message_id in
(select message0_.message_id
from .......
)
Notice the inner select on the OneToMany hit does not have a TOP associated with it. Therefore, it is pulling back ALL of the message reviews (instead of just the pertainent ones that JPA will need to match them up). Normally this is not a big issue, but with substantially large datasets in the OneToMany lists, the payload may return back millions of items from the database, when it only needs a very small subset for a given page.
Any ideas on how to get Paging to carry down to OneToMany lists in a JPA entity when utilizing @FetchMode.SubSelect to limit the number of SQL hits?