aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes How to write hasCode() & equals(0) methods in Compound Key Class    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to write hasCode() & equals(0) methods in Compound Key Class    " Watch "How to write hasCode() & equals(0) methods in Compound Key Class    " New topic
Author

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

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
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

Joined: Oct 20, 2006
Posts: 235
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30294
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

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 ]

My Blog: Down Home Country Coding with Scott Selikoff
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to write hasCode() & equals(0) methods in Compound Key Class