Fetch comments separately.
This avoids in-memory fetch limit application.
Change-Id: I37535d4e085a8be6602e45fe4a96df2b36176fd7
diff --git a/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java b/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java
index b0aa615..d522b1a 100644
--- a/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java
+++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java
@@ -222,9 +222,6 @@
cb = cb.where("post.scope").eq(Scope.top_level);
- cb = cb.leftJoinFetch("post.comments", "comment");
- cb = cb.fetch("comment.texts");
-
return cb;
}
@@ -373,8 +370,9 @@
}
}
- // Fetch texts (to avoid n+1 selects).
+ // Fetch texts and comments (to avoid n+1 selects).
fetchTexts(forwardResults);
+ fetchComments(forwardResults);
return new PostPage<>(prevCursor, cursor, nextCursor, forwardResults);
}
@@ -388,6 +386,15 @@
}
}
+ public static <T extends Post<?>> void fetchComments(Collection<T> posts) {
+ var postIds = posts.stream().map(x -> x.id).collect(toList());
+
+ if (!postIds.isEmpty()) {
+ find("SELECT p FROM Post p LEFT JOIN FETCH p.comments WHERE p.id IN (?1)", postIds).stream()
+ .count();
+ }
+ }
+
@CheckForNull
public Text getText() {
var texts = getTexts();