In reviewing the EJB 1.1 spec I don't find any mention that the bean must implement the equals and hashCode methods. However, the primary key class for an entity bean must implement these so they will return the appropriate values to tell whether two beans are identical or not. Was this what you were describing? John
The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Joined: Nov 03, 2000
Yes, this is what my doubt is but can u plese explain it.
Originally posted by Rahul Ghai: Yes, this is what my doubt is but can u plese explain it.
My guess would be that a typical J2EE server maintains an in-memory HashMap with all its entity beans mapped by primary key. That way, looking up an entity bean is fast and efficient. But for a HashMap to work, equals() and hashcode() must have sensible implementations. - Peter
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 public. All fields are declared as public. 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");
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com