I working on adding MCQ question, options and Answer.
There are three tables
Question
,Option
andAnswer
.
Below is my database schema
questionId (Question table) is primary key referred to by questionId (Option table) is foreign key.
questionId (Question table) is primary key referred to by questionId (Answer Table) is foreign key.
optionId (Option table) is primary key referred to by optionId (Answer table) is foreign key.
I am not able to link primary and foreign key - please help me improve.
QUESTION table:
@Entity
@Table(name = "mcq_question")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JoinColumn (name = "questionId")
private Long questionId;
@Column(nullable = false)
private String question;
@OneToOne(fetch = FetchType.LAZY)
@JoinTable( name = "Answer", joinColumns = @JoinColumn ( name = "question_id"), inverseJoinColumns = @JoinColumn( name = "option_id"))
private List <Option> options;
@OneToMany(mappedBy = "questionId", cascade = CascadeType.ALL)
private Set<Option> option;
public Question() { }
public Long getMcqId() {
return questionId;
}
public void setMcqId(Long mcqId) {
this.questionId = mcqId;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
@Override
public String toString() {
return "SessionMcqModel{" +
"mcqId=" + questionId +
", question='" + question + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Question question1 = (Question) o;
return questionId.equals(question1.questionId) &&
question.equals(question1.question) &&
options.equals(question1.options) &&
option.equals(question1.option);
}
@Override
public int hashCode() {
return Objects.hash(questionId, question, options, option);
}
}
ANSWER TABLE:
@Entity
@Table(name = "mcq_Options")
public class Option {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "option_id")
private int optionId;
private String option;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn
private Question questionId;
public Option() { }
public Option(String option, Question questionId) {
this.option = option;
}
public int getOptionId() {
return optionId;
}
public void setOptionId(int optionId) {
this.optionId = optionId;
}
public String getOption() {
return option;
}
public void setOption(String option) {
this.option = option;
}
public Question getQuestionId() {
return questionId;
}
public void setQuestionId(Question questionId) {
this.questionId = questionId;
}
@Override
public String toString() {
return "Option{" +
"optionId=" + optionId +
", option='" + option + '\'' +
", questionId=" + questionId +
'}';
}
}
ERROR:
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.bluepi.TrainingProject.model.Question.options references an unknown entity: java.util.List