The SchoolClass entity has a composite key made of schoolID, grade and section.
The key in student entity is a composite of userName and schoolID.
I am trying to add two foreign keys of student objects in my SchoolClass. I want to use the same column schoolID in SchoolClass table in all the mappings, am wondering if this is the correct way to do it or not ?
@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;
......
}
It is a better idea to simply remove composite keys in favor of an auto generated key on the entities involved.