首页 > 解决方案 > Spring Boot jpa - 与同一实体的 OneToMany 多重关系

问题描述

这是多字段关系的示例,但我想要一个具有多对一的实体关系,如下面的场景。

我想设计一个像stackoverflow这样的网站。

所以现在我发现了 3 个实体

  1. 问题
  2. 回答
  3. 评论

我将每个实体设计为

评论

public class Comment {
   private int commentId;
   private String content;
} 

回答

public class Answer {
   private int answerId;
   private String content;
   @OneToMany
   private List<Comment> comments;
} 

问题

public class Question {
   private int questionId;
   private String content;
   @OneToMany
   private List<Comment> comments;
   @OneToMany
   private List<Answer> answers;
} 

这里Comment的实体都提到了AnswerQuestion那么如何配置关系?

我对具有单一实体关系的 ManyToOne 或 OneToMany( Example1 , example2 ) 有一个想法

标签: javaspring-bootjpaone-to-many

解决方案


推荐阅读