• 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

Primary Key Class

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys..

why is it necessary whether BMP or CMP, to override the equals() and hashCode() methods in the Primary Key class.?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably because the container might use equals() to compare instances of the primary key class.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nischal Tanna:
Hi guys..

why is it necessary whether BMP or CMP, to override the equals() and hashCode() methods in the Primary Key class.?


I think the hashCode() should be overridden because the app server might be using the hash code of the primary key object to maintain the bean in a hash table, when it is cached in the server.
I remember one related issue in my last project, for some reason the value return by the primary keys hashCode() was not unique, this resulted in the finder method picking up the wrong bean, so we override the hashCode() method to return a the current time in millisecond ... this actually fixed the problem.
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The container must be able to compare keys to find equals - and must be able to store them in a map with them as the key and garauntee no dupes. Therefore 2 entity beans representing the same row of a table, gotten at different times, are seen as equal. this is most important in TX management, and data reliability under load.
Overriding hashcode to return the current millis is a HORRIBLE idea. It ruins the way that the hashmap should work and probably causes all sorts of unseen problems depending on the App Server!
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic