Adam Davies
Adam Davies

Reputation: 2812

JPA Lookup Table Values

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

Upvotes: 0

Views: 2206

Answers (2)

Perception
Perception

Reputation: 80633

You should look at this first from a database relational modeling point of view, before trying to map that relationship in JPA. First, you need to define how questions and answers are linked at the database level. I presume you use a foreign key or association table. Something like this:

CREATE TABLE questions (
    id INT NOT NULL PRIMARY KEY,
    text VARCHAR(255)
);

CREATE TABLE answers (
    id INT NOT NULL PRIMARY KEY,
    text VARCHAR(255)
);

CREATE TABLE question_answers (
    id INT NOT NULL PRIMARY KEY,
    question_id INT NOT NULL,
    answer_id INT NOT NULL,
    KEY k_question_id (question_id),
    KEY k_answer_id (answer_id),
    CONSTRAINT fk_question_answers_question_id FOREIGN KEY (question_id)
        REFERENCES questions(id),
    CONSTRAINT fk_question_answers_answer_id FOREIGN KEY (answer_id)
        REFERENCES answers(id)
);

This defines the relationship between questions and answers as an association table, which you can then map in JPA thusly:

@Entity(name="QUESTIONS")
public class Question {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private int order;
    private String questionTitle;
    private String questionText;

    @JoinTable(name = "question_answers", joinColumns = { @JoinColumn(name = "question_id", unique = true) }, inverseJoinColumns = { @JoinColumn(name = "answer_id") })
    private Set<AnswerOption> possibleAnswers;
}

The annotations for using a foreign key (as opposed to an association table) will be different of course, if you want to go that route leave a comment and I'll whip up an example. This should be enough to get you started.

Upvotes: 0

yair
yair

Reputation: 9255

As Hibernate is essentially an ORM tool, it just takes care of mapping your Question and AnswerOption classes 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 Question instances with their possible AnswerOption instances in some sort of initQuestions() initialization method.

Also, you'd better note whether these Questions are already initialized.

Upvotes: 1

Related Questions