使用@jointable,所有列都是唯一的

时间:2017-02-21 15:40:57

标签: mysql jpa jointable

我正在尝试创建一个调查。该调查由一系列问题组成。 每个问题依次由答案值集合组成。

我将问题与调查结合在一起:

@OneToMany()
@JoinTable(
        name = "survey_question_list",
        joinColumns = @JoinColumn(name = "survey_id"),
        inverseJoinColumns = @JoinColumn(name = "question_list_id"))
private Collection<Question> questionList = new ArrayList<Question>();

我以同样的方式将Answervalues与每个问题联系起来:

@OneToMany()
@JoinTable(
        name = "question_answer_value_list",
        joinColumns = @JoinColumn(name = "question_id"),
        inverseJoinColumns = @JoinColumn(name = "answer_value_list_id"))
private Collection<AnswerValue> answerValueList = new ArrayList<AnswerValue>();

表格是根据我的需要在数据库中创建的:与问题ID和问题ID相关联的调查ID与Answervalue Ids相关联。

我的问题是我收到以下错误:

enteorg.mariadb.jdbc.internal.util.dao.QueryException: Duplicate entry '1' for key 'UK_eky8oymsfv24p3r4by54m15ta' 
Query is: insert into question_answer_value_list (question_id, answer_value_list_id) values (?, ?), parameters [2,1]

我看到的问题是其中一个Ids是唯一的,它不允许多个相等的ID。 然而,我需要将每个问题映射到可能的答案,这意味着问题ID和答案ID必须多次出现。 例: 如果问题1和问题2有3个可能的答案,那么映射将如下所示:

Question_Id  Answer_Value_Id
1            1
1            2
1            3
2            1
2            2
2            3
etc.         etc.

如何更改表格以允许此操作?

编辑: 以下是我的调查构建的逻辑:

@RequestMapping("/createsurvey2")
public Survey createSurvey2() {
    Survey survey = new Survey();
    survey.setName("My First Survey");
    // surveyRepository.save(survey);

    AnswerValue answerValue1 = new AnswerValue(1);

    answerValueRepository.save(answerValue1);

    Question question1 = new Question();
    question1.setQuestionContent("Was the Meeting Helpful?");
    question1.addAnswerValue(answerValue1);

    questionRepository.save(question1);

    survey.addQuestion(question1);

    Question question2 = new Question();
    question2.setQuestionContent("Was the Meeting Horrible?");
    questionRepository.save(question2);
    question2.addAnswerValue(answerValue1);

    questionRepository.save(question2);

    survey.addQuestion(question2);

    Question question3 = new Question();
    question3.setQuestionContent("Was the Meeting Funny?");
    questionRepository.save(question3);
    question3.addAnswerValue(answerValue1);

    questionRepository.save(question3);

    survey.addQuestion(question3);


    surveyRepository.save(survey);

    return survey;
}

1 个答案:

答案 0 :(得分:0)

也许尝试以下方法:

em.getTransaction().begin();

Answer a1 = new Answer(1, "Federal");
Answer a2 = new Answer(2, "State");
Answer a3 = new Answer(3, "Local");

List<Answer> q1_answers = Arrays.asList(a1, a2, a3);
Question q1 = new Question(1, q1_answers);

List<Answer> q2_answers = Arrays.asList(a1, a2, a3);
Question q2 = new Question(2, q2_answers);

List<Question> questions = Arrays.asList(q1, q2);
Survey s = new Survey(1, questions);

em.persist(s);

em.getTransaction().commit();

在简单的Junit测试中使用它,我能够很好地创建下表:

QUESTION_ID ANSWER_VALUE_LIST_ID
1                   1
1                   2
1                   3
2                   1
2                   2
2                   3

注意:我在@OneToMany关系中使用了cascade = CascadeType.PERSIST:

@OneToMany(cascade = CascadeType.PERSIST)