Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

equals & hashcode implementation in B&S

 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am working on B&S 2.2.3. I have a transfer object and I have implemented equals & hashcode methods are as follows



I verified the following properties of equals method
[bold] symmetry [/bold]
[bold] reflexivity [/bold]
[bold] transitivity [/bold]
[bold] consistency with hashcode [/bold]

the first three properties are returning true and same hashcode is returned if two objects are equal. For transitivity , I used three objects.

Do I need to consider anything else?
Is the above implementation correct?

post your comments

sat
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi satishkumar,

I am just staring on my B&S (2.2.2) assignement and can't answer your question,however, I was just curious as to what scenario do you need to compare two instances of the Contractor object ?

Pete
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pete,

I have a Transfer object which contains the complete data for a single record. In a standalone environment, the possibility of creating the same record is almost zero. But in distributed environment, there might be a chance of getting multiple copies of the same record. Each copy will be deserialized into separate instantiation of the Transfer Object class. So here I need to differentiate the two objects hence I have overridden the equals & hashcode methods.
The hashcode must be implemented correctly if the Transfer Object is used as key in hash based collections.

sat
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satishkumar, I think you are doing more than the requirements, there is no requirement for the GUI to have functionality to create a new record, so why do you need to carry the object to the server.

Alain
 
Ranch Hand
Posts: 53
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satishkumar,

I am working on a different assignment, but I just use the record id for the equals method. This makes things realy easy.
However, in my assignment (URLyBird 1.3.2) two records are not equal if all data in the record is the same. I don't know if this is also the case for your data model, e.g. two contractors with the same name, location and speciality that are actually two different records in the db.

Cheers,
Ernesto
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all;

in my opinion; the record id is enough to uniqualy identify the contractor.

what you think guys ?

regards.
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all of you for posting your comments on this issue. Record ID is enough to identify the record provided the records position not changed in the db. If the records are added in the middle or deleted in the middle, we need to identify the record uniquely by concatenating name, location and speciality etc.

sat
 
I want my playground back. Here, I'll give you this tiny ad for it:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic