代码之家  ›  专栏  ›  技术社区  ›  StuartDTO

Spring JPA使用其他表引用表

  •  0
  • StuartDTO  · 技术社区  · 5 年前

    这是我的问题课

    @Entity(name = "question")
    public class Question extends DateAudit {
        @Id
        @Column(name = "question_id")
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "question_seq")
        @SequenceGenerator(name = "question_seq", allocationSize = 1)
        private Long id;
    
        @Column(name = "name")
        @NotBlank(message = "Question name can not be blank")
        private String name;
    
        @Column(name = "is_exam_question", nullable = false)
        private Boolean is_exam_question;
    
        @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
        private Set<Answer> answers = new HashSet<>();
    
    }
    

    这里我遗漏了一些东西,因为我想知道哪个用户回答了这个问题,以及他成功与否的次数。

    我的答案是

    @Entity(name = "answer")
    public class Answer {
    
        @Id
        @Column(name = "answer_id")
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "answer_seq")
        @SequenceGenerator(name = "answer_seq", allocationSize = 1)
        private Long id;
    
        @Column(name = "answer_to_question")
        @NotBlank(message = "Answer to question name can not be blank")
        private String answer_to_question;
    
        @ManyToOne
        private Question question;
    }
    

    解释得更好

    如何为问题添加多个答案以及如何将此答案分配给问题?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Muhammad Waqas    5 年前

    创建新类名为测验班

    @Entity
    public class Quiz{
    
    @Id
    private int id;
    
    private String quizName;
    
    private String quizDescription;
    
    private int passingScore;
    
    private int totalScore;
    
    // Getter and setters
    }
    

    问题.类会像这样

    @Entity(name = "question")
    public class Question extends DateAudit {
        @Id
        @Column(name = "question_id")
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "question_seq")
        @SequenceGenerator(name = "question_seq", allocationSize = 1)
        private Long id;
    
        @Column(name = "name")
        @NotBlank(message = "Question name can not be blank")
        private String name;
    
        @Column(name = "is_exam_question", nullable = false)
        private Boolean is_exam_question;
    
        @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE },mappedBy="question")
        private Set<Answer> answers = new HashSet<>();
    
        @ManyToOne
        private Quiz quiz;
    
    }
    

    @Entity
    public class Tests{
    
    private int id;
    
    @ManyToOne
    private Quiz quiz;
    
    @ManyToOne
    private User user;
    
    private int score;
    
    private String status; // failed or passed
    
    }
    

    我想这对你有帮助。请随意问我更多的问题。