Hi everyone,
I have 2 entities: a) Classroom (has many subjects) b) Subject (has 1 classroom)
public class Classroom { ... @OneToMany(mappedBy = "classroom") private Set<Subject> subjects = new HashSet<Subject>(); ... }
public class Subject {
Integer status = 1;
... @OneToOne @JoinColumn(name = "classroom_Id") private Classroom classroom; ... }
I want to change the Classroom to have only 1 subject or no subject depending if the subject's status = 1 (active vs 0=inactive) :
public class Classroom { ... @OneToOne(mappedBy = "classroom") private Subject> subject; ... }
But, a OneToOne mapping is only by FK. How do I also specify by an additional column and value eg 'subject.status = 1' ?
1. Can I provide HQL on the OneToOne mapping? 2. Or do I have to keep the ManyToOne and iterate manually over the Set<Subject> looking for the one with status=1? My issue with this, is that a Classroom can have 100s of subjects and I'd rather not fetch them all and then waste time iterating over them. 3. My mapping is completely wrong!!?
Many thanks for your time, J
|