Hi everyone,
in my project, we have manytomany associations of the form:
Code:
class Booking {
List<Wagon> wagons;
}
class Wagon {
List<Bookings> bookings;
}
I know that one side of a manytomany relation is the owning side and only
this side is used to update the association table.
Though updating one side of the association is enough, we would like to keep the object state consistent with the database
state to avoid misinterpretations.
As far as I know, in onetomany relations, the one-side's methods update the relation in both sides by convention such as:
Code:
class Person {
addDog(Dog d) {
mydogs.add(d);
d.setOwner(this);
}
}
Is there any similar convention to manage the object state of manytomany relations?
What I can think of is:
Code:
//owning side
class Booking {
List<Wagon> wagons;
addWagonsBidirectional(Wagon wg) {
wagons.add(wg);
wg.getBookings.add(this);
}
}
//inverse side
class Wagon {
List<Bookings> bookings;
getBookings() {
return bookings;
}
}
In that case we would (by convention) alwaws use the owning side as
responsible side for keeping relation state correct.
Many thanks in advance!