• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashcode() & equals() method in Entity Bean

 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



Thanx in advance
Shrinivas
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks
Shibu
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic