On 16 Jan 2004 13:20, lhotari wrote:
>On 16 Jan 2004 12:07, thorstenschaefer wrote:
>>Hi,
>>The commons-lang package from apache contains builder for equals and
>>hashCode (beside others) which can be used for efficent and straight-
>>forward implementation of these methods.
>http://commonclipse.sourceforge.net/ is an eclipse plugin that generates
>code for commons-lang builders.
The only problem I've found with commonclipse is that its references
member fields rather than getters and setters which can cause the
following issue.
Problem occurs when referencing objects internal field values with
Hibernated proxies. As the equals method has access to these private
fields it is only natural to directly access the fields. The problem is
that hibernate proxies will only fetch the fields value on request from
the fields getter.
incorrect equals()
/**
* (at) see java (dot) lang.Object#equals(Object)
*/
public boolean equals(Object object)
{
if (!(object instanceof User))
{
return false;
}
User rhs = (User) object;
return new EqualsBuilder().append(this.username,
rhs.username).isEquals();
}
correct equals()
/**
* (at) see java (dot) lang.Object#equals(Object)
*/
public boolean equals(Object object)
{
if (!(object instanceof User))
{
return false;
}
User rhs = (User) object;
return new EqualsBuilder().append(this.getUsername(),
rhs.getUsername()).isEquals();
} |