I've just been reading "Hibernate in Action"; it's an excellent book and has helped me a lot. It's definitely an essential complement to the reference docs.
Anyway, it says on p. 125 that equals() and hashCode() methods should use accessors (getters) instead of reading the "other" object's fields directly. This is all well and good (although I'm a little mystified about what's the "other" object in a hashCode() method).
My question concerns the compareTo() method in a "Comparable" domain object. Should this method (like equals() and hashCode()) use accessors instead of direct field access? I would have thought so, especially if the comparable object was being put into a sorted collection like a TreeSet. Yet there's no mention of such a requirement in either the book, the reference docs, or this forum. Is this just an oversight?
Thanks for any help,
Andrew
|