Entity Beans: equals and hashCode methods

Object equality can be checked using any of the two methods of the Object class and these are equals and hashCode. Because ,if two objects are same, equals should return a value of zero. Also, in this case hash codes of both the objects are same. My question is whether it necessary to write both equals and hashCode methods in the Entity Bean classes? Please illuminate.

[378 byte] By [maitya] at [2007-10-3 4:27:29]
# 1

Object.equals() returns a boolean. Therefore it returns true if two objects are equal. As to your specific question about Entity beans, I would actually implement the equals() and hashCode() methods in a Data Transfer Object (DTO). Entity beans are "heavyweight" instances and may contain Collections of other Entity beans as member variables. Iterating over these collections must be done inside transaction boundaries, so the Entity bean really shouldn't be used except to extract the data into the "lightweight" DTO.

With that caveat in mind, it makes perfect sense to implement the equals() and hashCode() methods for the Entity's DTO.

Brian

brian@cubik.caa at 2007-7-14 22:30:18 > top of Java-index,Core,Core APIs...
# 2
Generally if you override equals() you should also override hashcode(). See the docs for those methods or Josh Bloch's Effective Java.But this is the wrong forum for such questions.
davidholmesa at 2007-7-14 22:30:18 > top of Java-index,Core,Core APIs...