休眠中与映射的混淆

时间:2018-08-17 07:05:29

标签: java hibernate

我目前有3个表Assessment,Question和QuestionOption。每个评估都有一些问题,每个问题都有一些问题选项。最初,我从@OneToMany映射开始,但是后来由于我不得不在Question中使用复合键的字段而开始使用,因此整个映射都发生了变化。我对目前的状态不满意。有人可以帮我映射表格吗?

我必须这样做的主要原因是,在插入问题时,我意识到在多个评估中问题编号可以相同。因此,我将其与questionNumber + assessmentmentId组合在一起。同样的思考过程与问题选项。相同的问题选项可以存在于不同的问题中。如何避免这种情况并保持简单。

评估

@Entity
@JsonIgnoreProperties({"questionList"})
public class Assessment {

    @Id
    private String assessmentId;

    private String assessmentTopic;

    private String assessmentSubTopic;

    private String assessmentLevel;

    private String createdBy;

    private String rating;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assessment", fetch = FetchType.LAZY)
    private List<Question> questionList;
 //Getters and setters
}

问题

@Entity
@Getter
@Setter
@JsonIgnoreProperties({"assessment"})
public class Question implements Serializable {

    @EmbeddedId
    private QuestionAssessmentKey questionAssessmentKey;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name =  "assessmentId", insertable = false, updatable = false)
    private Assessment assessment;

    private String questionText;

    private String questionURL;

    private QuestionStatus questionStatus;

    //    private int questionNumber;
    private QuestionTypes questionType;
  //Getters and Setters
}

QuestionAssessmentKey-复合键

@Embeddable
public class QuestionAssessmentKey implements Serializable {

    private int questionNumber;

    private String assessmentId;
 //Getters and setters
}

QuestionOption

@Entity
public class QuestionOption {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int questionOptionId;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumns({
            @JoinColumn(name="assessmentId", referencedColumnName = "assessmentId"),
            @JoinColumn(name="questionNumber", referencedColumnName = "questionNumber")
    })
    private Question question;

    private Character questionOption;

    private String questionOptionText;

  //Getters and Setters
}

0 个答案:

没有答案