Let's say you have classes in a tree like below:
(the getters and setters have been omitted.)
Code:
class Country {
String code;
Set provinces; // of type Province
}
class Province {
String code;
Set cities; // of type City
}
class City {
String code;
}
Seems like a simple tree. If I were writing this data model just for Java then my equals and hasCode methods would traverse the children when computing equality. The Country.equals() method would compare codes then ensure that they contain the same number of provinces then call the equals method on all it's provinces. The Province.equals() method would ensure the codes are equal then check the number of cities then call the equals method on all of it's cities. The City.equals() method would be atomic and only check that the codes are equal.
Would there be any issues with doing this when the tree is loaded/stored using Hibernate? Should I just be checking primary keys (which might break the equals method contract)?
Thanks,
-alan