在外键约束中获取重复的ID问题

时间:2019-03-11 13:04:38

标签: mysql spring-data-jpa foreign-keys

我有一个测验应用程序,具有类别,问题和答案作为实体。一类可以有多个问题,而一个问题可以有多个答案。这是我的课程,

Category.java

@Entity
@Table(name = "categories")
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long catId;

    @Column(nullable = false , unique = true)
    private String description;

Question.java

@Entity
@Table(name = "question")
public class Question {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long questionId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "catId", nullable = false, updatable = false)
    @JsonBackReference
    private Category category;

Answer.java

@Entity
@Table(name = "answers")
public class Answer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long answerId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "questionId", nullable = false, updatable = false)
    @JsonBackReference
    private Question question;

所以我创建了一个类别,可以在其中插入几个问题,但是当它回答时会说

"Duplicate entry '3' for key 'UK_a8hukb7yjph7wnrlnn8x0fcja'"

我看不到上述两种情况之间的任何关系差异,请提供帮助。

0 个答案:

没有答案