SchoolClass 实体有一个由 schoolID、grade 和 section 组成的复合键。学生实体中的键是 userName 和 schoolID 的组合。我正在尝试在我的 SchoolClass 中添加两个学生对象的外键。我想在所有映射中的 SchoolClass 表中使用相同的 schoolID 列,我想知道这是否是正确的方法?
@Entity
public class SchoolClass {
@Column(unique=true, nullable=false)
@EmbeddedId
private SchoolClassPK key;
@ManyToOne(optional = true)
@JoinColumns({
@JoinColumn(name="classRep1", referencedColumnName="userName", nullable = true),
@JoinColumn(name="schoolID", referencedColumnName="schoolID", nullable = true),
})
private Student classRep1;
@ManyToOne(optional = true)
@JoinColumns({
@JoinColumn(name="classRep2", referencedColumnName="userName", nullable = true),
@JoinColumn(name="schoolID", referencedColumnName="schoolID", nullable = true),
})
private Student classRep2;
......
}