I have an entity that has a reference to another one like these:
class School {
private boolean used = false;
}
class Student {
@ManyToOne
private School school;
}
The attribute used indicates that the School entity is whether used, referenced or not. So when created, a School entity should have used false, but once a Student makes a reference to it, the used must be turned to true. Is there any automatically way to do this like triggers in database?
I try to use @PrePersist and @PostPersist on Student entity like this but it doesn’t work:
@PrePersist
public void prePersist(){
school.setUsed(true);
}
Thanks,
My thought is that School should also have a reverse list of students. i.e.
So when the school is loaded, you can easily access the list of students. Then finding out if a school is being used becomes very simple. You will not longer need a boolean flag, just this: