我为我试图实现的代码编写了一个示例,我收到一个错误,约束“Student_Teacher_FK”已经存在。
@embiddable 类有一个用当前代码创建两次的外键。
@Entity
public class Teacher {
@Id
@GeneratedValue
private Long id;
@Column(name = "Name")
private String name;
}
@Entity
public class Student{
@Id
@GeneratedValue
private Long id;
@Column(name = "Name")
private String name;
}
@Embeddable
public class StudentList implements Serializable {
@ManyToMany
@JoinTable(name = "Student_Teacher",
joinColumns =
@JoinColumn(name = "Student_ID", referencedColumnName = "ID"),
inverseJoinColumns =
@JoinColumn(name = "Teacher_ID", referencedColumnName = "ID")
)
@ForeignKey(name = "Student_Teacher_FK", inverseName = "Teacher_Student_FK")
public List<Student> studentList = new ArrayList<Student>();
}
@Entity
public class HistoryTeacher extends Teacher {
@Embedded
@NotNull
private StudentList StudentList = new StudentList ();
}
@Entity
public class LangTeacher extends Teacher {
@Embedded
@NotNull
private StudentList StudentList = new StudentList ();
}
@Entity
public class RetiredTeacher extends Teacher {
// has no students
}