• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding overriding of hashcode() and equals() method in Hibernate.

 
Tusshar Fasate
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
I am having Activity domain class and fields are like this:

private Integer activityID;
private String activityName;
private Integer type;
private Application application;
private Project project;
private ActivityConfigurable activityConfigurable;
private ActivityNonConfigurable activityNonConfigurable;
private Status status;
private Set activityScripts;

activityID is the primary key in database.
Can somebody tell me which fields should I use while overriding hashcode() and equals() methods?
I read somewhere that one should not use primary key of that table in these methods.
Why is it so?

Thanks in advance.
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should user those fields that form some kind of business key. If you use the technical ID you cannot compare a newly created instance with a persisted one.
 
Tusshar Fasate
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christian Dillinger for your reply.
But can you help me for my example?
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which combination of fields does identify you "Activity" uniquely? That's a business key.

Let's take the following example:

Customer:
- Long custId;
- String firstName;
- String lastName;
- String eMail;
- Date birth;
- int size;
- String phone;

Which attributes do identify your customer?
firstName/lastName isn't enough I think. firstName/lastName/birth could enough, but who can guarantee that there won't be two John Smith, born December 24th?
phone isn't enough as well.
I'd choose eMail as a business key (I know this has some weeknesses too, but for an example...), so my implementation uses the eMail in equals and hashCode.
 
Tusshar Fasate
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your reply.
It cleared my doubt up to certain extent.
In your example,can we use combination of
String firstName;
- String lastName;
- String eMail;
- Date birth;
- int size;
- String phone;
these fields?

Please answer.

 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you could, but I wouldn't because "size" is just a fun attribute. You can use every combination that has "a business meaning" and identifies your domain object uniquely.
 
Tusshar Fasate
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks once again...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic