Here's an excerpt of the official Hibernate tutorial
First, keep in mind that Hibernate does not affect normal Java semantics. How did we create a link between a Person and an Event in the unidirectional example? You add an instance of Event to the collection of event references, of an instance of Person. If you want to make this link bi-directional, you have to do the same on the other side by adding a Person reference to the collection in an Event. This process of "setting the link on both sides" is absolutely necessary with bi-directional links.
Many developers program defensively and create link management methods to correctly set both sides (for example, in Person):
protected Set getEvents() {
return events;
}
protected void setEvents(Set events) {
this.events = events;
}
public void addToEvent(Event event) {
this.getEvents().add(event);
event.getParticipants().add(this);
}
public void removeFromEvent(Event event) {
this.getEvents().remove(event);
event.getParticipants().remove(this);
}
What does the word "absolutely" mean in this case? :
- Meaning it's recommended in order to keep the relationship consistent as long as the current logic process finishes, but persistence obviously can occur ?
- Meaning that without setting the other side (not owning the relationship), the persistence can strictly not be made ?
In other words, what would happen if my "adding" method is:
public void addToEvent(Event event) {
this.getEvents().add(event);
//event.getParticipants().add(this); without this line
}