我有2个表的问题和问题选项。问题具有复合键。当我通过ID查询问题时,如何获得问题选项。如何确保我也能获得问题选项。到目前为止,我只得到问题。我应该更改映射还是应该添加一些属性
问题
@Entity
@Getter
@Setter
@JsonIgnoreProperties({"assessment"})
public class Question implements Serializable {
@EmbeddedId
private QuestionAssessmentKey questionAssessmentKey;
public QuestionAssessmentKey getQuestionAssessmentKey() {
return questionAssessmentKey;
}
public void setQuestionAssessmentKey(QuestionAssessmentKey questionAssessmentKey) {
this.questionAssessmentKey = questionAssessmentKey;
}
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "assessmentId", insertable = false, updatable = false)
private Assessment assessment;
private String questionText;
private String questionURL;
private QuestionStatus questionStatus;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="assessmentId", referencedColumnName = "assessmentId"),
@JoinColumn(name="questionNumber", referencedColumnName = "questionNumber")
})
private List<QuestionOption> questionOptions;
public List<QuestionOption> getQuestionOptions() {
return questionOptions;
}
public void setQuestionOptions(List<QuestionOption> questionOptions) {
this.questionOptions = questionOptions;
}
public Assessment getAssessment() {
return assessment;
}
public void setAssessment(Assessment assessment) {
this.assessment = assessment;
}
// private int questionNumber;
private QuestionTypes questionType;
//Getters and setters
}
QuestionOptions
@Entity
public class QuestionOption {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int questionOptionId;
@ManyToOne
@JoinColumns({
@JoinColumn(name="assessmentId", referencedColumnName = "assessmentId"),
@JoinColumn(name="questionNumber", referencedColumnName = "questionNumber")
})
private Question question;
private Character questionOption;
//Getter and setter
}
QuestionAssessmentKey
@Embeddable
public class QuestionAssessmentKey implements Serializable {
private int questionNumber;
private String assessmentId;
}
答案 0 :(得分:0)
同样,您无法在单个查询中获取所有数据,因为它是问题和问题选项之间的一对多关系。但是,当您在获取的getQuestionOptions
实体上调用Question
时,它应该加载并返回相应的选项集。