gpt4 book ai didi

java - 如何在 spring data jpa 中创建一对多映射的嵌套投影

转载 作者:行者123 更新时间:2023-12-01 18:00:34 25 4
gpt4 key购买 nike

我有一个与 post 和 post_comments 表的一对多映射,我们的要求是仅检索两个表中的几个值,并作为一对多映射(如 postDTO)发送回调用者。下面是我们的代码。

发布实体

@Entity(name = "Post")
@Getter
@Setter
public class Post {

@Id
private Long id;

private String title;

private LocalDateTime createdOn;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "post", orphanRemoval = true)
private List<PostComment> comments = new ArrayList<>();

public void addComment(PostComment comment) {
this.comments.add(comment);
comment.setPost(this);
}

}

PostCommentEntity

@Getter
@Setter
public class PostComment {

@Id
private Long id;

private String review;

private LocalDateTime createdOn;

public PostComment(String review) {
this.review = review;
this.createdOn = LocalDateTime.now();
}

@ManyToOne
private Post post;

}

postDTO --> 我们需要的期望响应格式。

@Getter
@Setter
@Builder
@ToString
public class PostDTO {

String title;

@Builder.Default
List<PostCommentsDTO> comments;
}

PostCommentsDTO --> 一对多嵌套投影值。

@Data
@Builder
public class PostCommentsDTO {

String review;

}

因为我们无法直接使用 spring data jpa 来实现这一点。使用替代映射实现。

PostRepository 我们只需要从 post 表中获取标题,从 postcomment 表中获取评论作为 postDTO 类,因为我们无法在单个实例中执行映射,我将 Java 中的映射委托(delegate)如下:创建中间投影。

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {

@Query("SELECT p.title as title, c.review as review FROM Post p JOIN p.comments c where p.title = :title")
List<PostCommentProjection> findByTitle(@Param("title") String title);
}

PostCommentProjection

public interface PostCommentProjection {

String getTitle();

String getReview();

}

最后在 Java 中

    List<PostCommentProjection> postCommentProjections = this.postRepository.findByTitle("Post Title");

final Function<Entry<String, List<PostComments>>, PostDTO> mapToPostDTO = entry -> PostDTO.builder()
.title(entry.getKey()).comments(entry.getValue()).build();
final Function<PostCommentProjection, String> titleClassifier = PostCommentProjection::getTitle;
final Function<PostCommentProjection, PostComments> mapToPostComments = postCommentProjection -> PostComments
.builder().review(postCommentProjection.getReview()).build();
final Collector<PostCommentProjection, ?, List<PostComments>> downStreamCollector = Collectors
.mapping(mapToPostComments, Collectors.toList());

List<PostDTO> postDTOS = postCommentProjections.stream()
.collect(groupingBy(titleClassifier, downStreamCollector)).entrySet().stream().map(mapToPostDTO)
.collect(toUnmodifiableList());

是否有有效或自动的方法直接从存储库获取 POSTDTO 项目?

最佳答案

由于您已经有了标题,所以只缺少评论,因此仅搜索评论,然后将它们与标题结合起来。

此外,您按标题搜索评论有点奇怪。标题并不唯一,可能有 10 个不同的帖子具有相同的标题,因此您将获取所有 10 个帖子的评论。我建议您通过帖子 ID 搜索评论。

@Repository
public interface PostCommentRepository extends JpaRepository<PostComment, Long> {

@Query("select review from PostComment where post.id = :postId")
List<String> findAllReviewsByPostId(Long postId);
}

... main() {
...
List<String> reviews = commentRepo.findAllReviewsByPostId(post.getId());
PostDTO dto = new PostDTO(post.getTitle(), reviews);
}

关于java - 如何在 spring data jpa 中创建一对多映射的嵌套投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60641117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com