JFYI in our project we ended up with ID-based equals/hashCode to avoid instantiation of lazy/proxy objects when they are get inserted into Set for example.
But like you we needed a way to compare objects property-by-property which was needed for Unit tests only. Our solution may... seem strange at least :) We made all our classes Comparable and their compareTo method performs property-by property comparison.
So when we need to check if object is really the same, we use
object.compareTo(another) == 0
Below an example of a class
Code:
public class Task implements Comparable
{
private Long id;
private String name;
private Double estimatedCost;
private Company company;
/* Comparison */
public int hashCode()
{
return (id == null) ? 0 : id.hashCode();
}
public boolean equals(Object object)
{
if (object == this)
return true;
else if ( !(object instanceof Task) )
return false;
Task other = (Task) object;
return Util.equals(id, other.getId());
}
public int compareTo(Object object)
{
if (object == this)
return 0;
Task other = (Task) object;
int result;
result = Util.compare(id, other.getId());
if (result != 0)
return result;
result = Util.compare(name, other.getName());
if (result != 0)
return result;
result = Util.compare(estimatedCost, other.getEstimatedCost());
if (result != 0)
return result;
result = Util.compareAssociation(company, other.getCompany());
if (result != 0)
return result;
return result;
}
...
}