• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Entity Bean

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Why are we required to override the the equals() and the hashcode() methods in the Entity Bean? If possible kindly explain in detail.
Thanks.
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rahul Ghai
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this is what my doubt is but can u plese explain it.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic