I have a class A that has a many-to-1 relationship with B and 1-to-many relationship with class C.
Assume tables with class names exist. C has a foreign key to A. and A as a foreign key to B.
Now by business needs are that when an instance of A is deleted, the FK column values referencing the A instance in table C set to null. And the entry in the A table is deleted.
Since we have to maintain the object relationships manually, I do the following (assume 'a' is associated with two 'c' instances)
Code:
a.removeC(c1);
a.removeC(c2);
c1.setA(null);
c2.setA(null);
a.setB(null);
b.removeA(a);
aDAO.remove(a);
And this works fine. Now the question I have is should I move this logical sequence of maintaining the object relationships under the hoods of the DAO remove() method? Or should this sequence of code be in the service method that "deletes" an instance of A? What if multiple services need to carry out the same "delete"..
Most of the examples I've seen have separate methods on each entity to maintain the relationship with another entity and the DAO simply calls delete. However in the case I described, for a remove operation, the logical unit of relationship is across three classes and not two.
Thanks,
Sanjiv
Code:
class A
{
private B b;
private Set cees;
}
class B
{
private A a;
}
class C
{
private A a;
}