我有三个要链接的表
学生(id,名字,姓氏)->存储学生详细信息
问题(id,问题,答案)->使用正确的答案存储问题
答案(id,qid,answer)->存储问题ID和学生给出的答案
将使用@OneToMany将学生和答案链接起来
除了FK之外,我们还可以具有@OneToMany的属性吗,以便我可以在其中包含问题ID和给定的答案
@Entity
@Table(name = "answer")
@SecondaryTable(name = "question",pkJoinColumns = {
@PrimaryKeyJoinColumn(name="qid",referencedColumnName = "qid")})
public class AnswerEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "qid")
private long qid;
@Column(name = "answer")
private String answer;
}
@Entity
@Table(name = "question")
@Inheritance(strategy = InheritanceType.JOINED)
@Component
public class QuestionEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "qid", updatable = false, nullable = false, unique = true)
private long qid;
...
}
@Entity
@Table(name="student")
public class Student{
...
@OneToMany(fetch = FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinTable(name = "has_answered", joinColumns = {@JoinColumn(name = "student_id", referencedColumnName = "uid")}, inverseJoinColumns = {@JoinColumn(name = "answer_id", referencedColumnName = "id")})
private List<AnswerEntity> answerEntityList;
...
}
我收到此错误消息-
org.hibernate.AnnotationException:SecondaryTable JoinColumn无法引用非主键