Equals and hashcode Approach/ Implementation for Business Objects (Bo)

A common source for bugs while writing code Business Objects for relational database using Object Relation Mapping tools is the implementation of the Equals and Hashcode methods.

If you would like to know more about the Equals and Hashcode importance for an ORM Click here.

To summarize the ORM implementation, There are two types of keys: Technical Keys and Business keys.

Technical keys are the primary key on the for a table to uniquely identify a database row, these numbers are auto generated sequence number (Item Unique IDentification/ IUID).

Business keys are the Business primary (/ composite) keys which uniquely identify the business objects in the database tables.

One of the important factor while writing a equals and hashcode method is to decide on what attributes needs to go into these methods; Some guidelines for deciding the attributes for Equals and hashcode methods.

Implementation wise, Apache commons-lang has a very good implementation for hashcode and equals method. Here’s an example of equals and hashcode methods using the apache commons-lang.

@Entity
@Table(name = "APP_CONFIGURATION")
@SequenceGenerator(name = "OPS_SEQ", sequenceName = "SQ_CONFIGURATION_CNTR")
public class ConfigurationBo extends AbstractBo {

private static final long serialVersionUID = -5098377177228374794L;

@Column(name = “ITEMNAME”, length = 100)
private String name;

@Column(name = “STRINGVALUE”, length = 100)
private String stringValue;

@Column(name = “INTVALUE”)
private Integer intValue;

@Column(name = “TYPENAME”, length = 20)
private String type;

public Integer getIntValue() {
return intValue;
}

public void setIntValue(Integer intValue) {
this.intValue = intValue;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getStringValue() {
return stringValue;
}

public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj.getClass() == this.getClass())) {
return false;
}
ConfigurationBo other = (ConfigurationBo) obj;

return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(this.name, other.name)
.append(this.type, other.type)
.isEquals();
}

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return new HashCodeBuilder()
.appendSuper(super.hashCode())
.append(this.name)
.append(this.type)
.toHashCode();
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return super.toString() + “[name=” + name + “, type=” + type + “]”;
}

}

Written by Ravi Nallakukkala on April 15th, 2007 with 4 comments.
Read more articles on Design and Hibernate.

Related articles

4 comments

Read the comments left by other users below, or:

Get your own gravatar by visiting gravatar.com Daniel
#1. May 6th, 2007, at 1:07 AM.

I wouldn’t include the technical key in hashcode calculation or equals comparison. Sometimes the object to check is not stored in the database so technical key is not set (null) but the business key can be equal so it won’t work.

Greetings from Atlanta :-)

Get your own gravatar by visiting gravatar.com Ravi Nallakukkala
#2. May 7th, 2007, at 11:10 PM.

Hey Daniel,

Greeting to you from hot summer land; Phoenix :)

Say, we have two object with identical values; one object stored in Database (with technical key) and another which is not stored in database (without a technical key). In this case, if you don’t include technical keys in hash code and equals methods, Hibernate execution will lead to undesired results.

I totally agree with if removing technical keys in equals/ hash code if I’m working on Dto objects than a Business Objects.

If you are concern about handling of null cases, commons-lang package takes care of it.

Thanks

Get your own gravatar by visiting gravatar.com Daniel
#3. May 7th, 2007, at 11:39 PM.

Are you sure about the undesired results. Even Hibernate recommends using only the “business key” for equals and hashcode [1].

[1] http://www.hibernate.org/109.html

Get your own gravatar by visiting gravatar.com Ravi Nallakukkala
#4. May 7th, 2007, at 11:55 PM.

Well, The recommendation was use a combination of Technical and business keys (and “not” Technical keys alone)

“Include the Technical Keys and Business keys in equals and hashcode methods.”

Worst case scenario will be when your business keys are not unique.

Thanks

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:




You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> .