作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个与 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/
我是一名优秀的程序员,十分优秀!