File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Entity Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Entity Bean" Watch "Entity Bean" New topic

Entity Bean

Rahul Ghai

Joined: Nov 03, 2000
Posts: 21
Why are we required to override the the equals() and the hashcode() methods in the Entity Bean? If possible kindly explain in detail.
John Wetherbie

Joined: Apr 05, 2000
Posts: 1449
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?

The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Rahul Ghai

Joined: Nov 03, 2000
Posts: 21
Yes, this is what my doubt is but can u plese explain it.
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1868
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)
if (key1.hashCode(key2) == 0)
I agree. Here's the link:
subject: Entity Bean
It's not a secret anymore!