Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Primary Key Class  RSS feed

 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • 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.?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because the container might use equals() to compare instances of the primary key class.
 
Vinod John
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • 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.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!