I’ve got 2 tables. One for questions and one for possible answers. For example, I’m modelling:
“Do you own a dog?” Yes[ ], No[ ].
So I have a set of questions and a set of possible answers. What I want to know how do I represent this in JPA (note this is not about capturing the answer, but displaying the question and populating a selection box).
So far I have:
@Entity(name="QUESTIONS")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private int order;
private String questionTitle;
private String questionText;
private Set<AnswerOption> possibleAnswers;
....
}
It is the private Set<AnswerOption> possibleAnswers; part I’m having trouble with. How do I get this to be pre-populated with the possible range of answers?
The way it is modelled above will provide a Set variable to store answers in.
Am I thinking about this the wrong way? Should I use code to populate the database and assign the same AnswerOption object(s) to different Question objects?
Thanks for any help.
Adam
As Hibernate is essentially an ORM tool, it just takes care of mapping your
QuestionandAnswerOptionclasses and instances to the defined tables.What you need is to initialize data, not data structure. So, you’ve got to populate all of your
Questioninstances with their possibleAnswerOptioninstances in some sort ofinitQuestions()initialization method.Also, you’d better note whether these
Questions are already initialized.