代码之家  ›  专栏  ›  技术社区  ›  Sasikumar Murugesan

Spring Boot JPA-同一实体的多个关系

  •  0
  • Sasikumar Murugesan  · 技术社区  · 6 年前

    This is example for multiple field relationship 但我想要一个多实体的实体关系,有很多人,就像下面的场景。

    我想设计一个像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 两者均指实体 Answer Question 那么如何配置关系呢?

    我和一个男人或一个女人有个主意( Example1 , example2 )具有单一实体关系

    0 回复  |  直到 6 年前