Consider the following code in Lecturer.java
. The setter in Lecture.java
has the exact same form. Please also note that the attribute of interest is private
.
public void setLecture(Lecture lecture) {
if (this.lecture == lecture) return;
if (this.lecture != null) {
this.lecture.setLecturer(null);
}
this.lecture = lecture;
if (this.lecture != null) {
this.lecture.setLecturer(this);
}
}
This results in an infinite loop when trying to set a null
value that has previously not been null
.
I can't believe it's this hard to keep a 1:1 relation consistent - but I just can't figure out how to do so. How would one solve this issue?
Finally, here's your solution:
My Test Class: