Hi,
The Primary Key must be an Object not an primitive type. If you want the Primary key to be a primitive type then you must use the appropriate wrapper classes. For example the wrapper class for int primitive type is the Integer. All/Most the
java classes override the equals() and the hashCode() methods. If you want to specify your own specific Primary Key class then the following apply for that class.
A primary key class must meet these requirements:
The access control modifier of the class is coderanch.
All fields are declared as coderanch.
For container-managed persistence, the field names in the primary key clsss must match the corresponding container-managed fields in the entity bean class.
The class has a public default constructor.
The class implements the hashCode() and equals(Object other) methods. The class is serializable.
Lets just check up the equals() and the hashCode() methods
The default implementation of the equals method of an object would check if two object references are the same. If so then returns true else false. as the API specifies
"The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true)."
Since an ejb bean must check up the equals() based on an algorithm so we need to override this method.
The Java Docs also specify the following about hashCode()
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
Thus when you override the equals() method then you must also override the hashCode() method to see to it that the above specification is held true.
These two methods can be used by the client to fetch and compare the primary keys of two entity beans:
Account accta, acctb; (where Account class implements EJBObject)
MyPrimaryKey key1 = (MyPrimaryKey)accta.getPrimaryKey();
MtyPrimaryKey key2 = (MyPrimaryKey)acctb.getPrimaryKey();
if (key1.equals(key2) == 0)
System.out.println("equal");
OR
if (key1.hashCode(key2) == 0)
System.out.println("equal");