From 66d49a3f76728945a0b8221bbcc6c54612967b9c Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 29 Aug 2023 16:24:32 +0200 Subject: Fetch comments separately. This avoids in-memory fetch limit application. Change-Id: I37535d4e085a8be6602e45fe4a96df2b36176fd7 --- src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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 @@ public abstract class Post> extends PanacheEntityBase { 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 @@ public abstract class Post> extends PanacheEntityBase { } } - // 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 abstract class Post> extends PanacheEntityBase { } } + public static > void fetchComments(Collection 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(); -- cgit v1.2.3