Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to write hasCode() & equals(0) methods in Compound Key Class

 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am wondering how to write the hashCode() and equals() methods that is mandatory when creating composite

@IdClass. Below is the combination of primary @Id keys that I would like to make up in a compound key class:


public final class PatientKey implements java.io.Serializable
{
private String firstnameId;
private String SurnameId;
private String DateOfBirthID;
private String Sex;

public int hashCode()
{
.....
}

public boolean equals(Object otherOb)
{
......
}

......
}


The Order application in Java EE 5 Tutorial (page 841) is made up of only 2 primary keys.

Below is an example of the primary compound class key I am trying to replicate. It is the LineItemKey class that is part of the Order application from the same tutorial:


public class LineItemKey implements Serializable {
private Integer orderId;
private int itemId;

public LineItemKey() {
}

public LineItemKey(
Integer orderId,
int itemId) {
this.setOrderId(orderId);
this.setItemId(itemId);
}

public int hashCode() {
return (((this.getOrderId() == null) ? 0 : this.getOrderId()
.hashCode())
^ ((int) this.getItemId()));
}

public boolean equals(Object otherOb) {
if (this == otherOb) {
return true;
}

if (!(otherOb instanceof LineItemKey)) {
return false;
}

LineItemKey other = (LineItemKey) otherOb;

return (((this.getOrderId() == null) ? (other.getOrderId() == null)
: this.getOrderId()
.equals(other.getOrderId()))
&& (this.getItemId() == other.getItemId()));
}
.....
}

These methods are making comparison between 2 objects based on their properties. I would like to achieve the same thing in an identical compound class (@IdClass) based on at least 4 or more properties. ie firstname, surname, dob and sex.

Thanks,

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

Could anyone provide some suggestion on how this could be done?

I am still at a loss on how to do this.

Thanks alot,

Jack
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack,
There's nothing EJB specific about these methods. They are just normal Java equals() and hashCode() methods. Note that EqualsBuilder provides dynamic implementations so you don't have to write your own.
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Thanks for responding to this query.

I am wondering if you could confirm whether the equals() and hashcode() for the following PatientPK composite class is correct:


Thanks again,

Jack
 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like the looks of "if (obj == this) return true;" inside of equals() method, it seems like a bad idea.

Other than that, this really is not an EJB issue, its more of a general java question, so I'm moving it to Java Intermediate. You should read up on equals() methods. Usually in a database environment you have a unique primary key (String, Integer, or Long) that unique determines the object.
[ May 29, 2008: Message edited by: Scott Selikoff ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic