Thursday, September 10, 2009

Override equals/hashcode in Hibernate domain object

First of all, make sure that you really need to do that. According to this post,
"You only need to implement equals()/hashCode() on your persistent classes if
- it is a used as a composite primary key class
- instances of that class, loaded in different Hibernate Sessions, are in the same Set (or should be compared)"

When you do, be careful as you might run into a tricky problem. If you were me, you would use Eclipse's wizard to create equals/hashcode, which generates code including the below

if (getClass() != other.getClass()) {
return false;
}

The problem is that Hibernate domain objects are proxies, by Javassist for example. This could return false as one is the domain class and the other is the proxy class. A workaround can be found here.

Again, equals/hashcode are very important and don't take a light decision on overriding them. Use this Hibernate page as reference.

No comments:

Post a Comment