Hello Everybody, Can anybody help me in solving my doubt: 1) I am not very clear about why we have to write a hashcode () & equals() method in Primary Key Class of any type of entity bean. 2) I think for a single field primary key even if i don't write a primary key class it works fine but for composite key the primary key class is must.
Q1) You need to provide meaningful implementation for your hashcode and equals method if you are using custom primary class. This is required in order to obey the general contracts between java objects. In this case each of your primary key instance is an object and it needs to obey the object contracts. To learn more about Java Object Contracts, take a look in to Chapter 3 of Effective Java Book; which explains all these in detail.
Moreover, most of the collection classes and algorithms related to Collection classes depend heavily on the equals. So if you havent provided meaningful implementation of these methods according to your scenario, you wont get meaningful results in your program. In most cases, even you will get very funny results too.
With regard to CMP EJB's, I think most of the finder methods use collection classes to return the correct instance of the object. So it is very much required to implement the above said funtions.
Q1) If you are using a single primary key, you will be mostly dealing with generic data types like a string or int or even dates. For all these generic data types java knows how to handle meaningfully the above functions. In other words the implementation for toString(), hashCode() equals() is already available with Java and that will hold good in all cases. This is the reason you dont need to have your own implementation for these cases.