diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2023-08-29 16:24:32 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2023-08-29 16:24:32 +0200 |
commit | 66d49a3f76728945a0b8221bbcc6c54612967b9c (patch) | |
tree | c95dc11e578a56112ca58a21af3418772736b150 /src | |
parent | 120a5ac3bfd2747cc68de8a1398a3b43a1795713 (diff) |
Fetch comments separately.
This avoids in-memory fetch limit application.
Change-Id: I37535d4e085a8be6602e45fe4a96df2b36176fd7
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java | 15 |
1 files 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<Text extends PostText<?>> 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<Text extends PostText<?>> 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<Text extends PostText<?>> extends PanacheEntityBase { } } + 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(); |