I've implemented a custom hashCode (and equals) method based on the id of my persistent object.
This hashCode method works, even if it's a proxy.
Code:
@Override
public int hashCode()
{
// getClass() is not hashbaar
return new HashCodeBuilder()
.append(getId())
.toHashCode();
}
@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o instanceof JpaIdentifiable)
{
JpaIdentifiable other = (JpaIdentifiable) o;
if (getId() == null || other.getId() == null)
{
return false;
}
return getId().equals(other.getId())
&& isSameClassIgnoringProxySubclass(other);
} else {
return false;
}
}
The problem is, that hibernate insists on intializing this object anyway when hashCode() is called. At that point it's detached in my swing client and I get a LazyInitializationException of course.
From the hibernate manual:
Code:
Certain operations do not require proxy initialization
*
equals(), if the persistent class does not override equals()
*
hashCode(), if the persistent class does not override hashCode()
*
The identifier getter method
Hibernate will detect persistent classes that override equals() or hashCode().
Can I tell hibernate "yes I 've overridden the hashCode() method, but I still don't want you to require proxy initialization for it"?
Thanks for any and all help :)