You explain at https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch04.html#persistent-classes-equalshashcode how to write equals and hashcode. Current java versions allow to implement these methods in much shorter and cleaner code using . {{ java.util.Objects.equals() }} and {{ java.util.Objects.hash() }} . E.g. {code:java} @Override public boolean equals(Object rhs) { if (this == rhs) return true; /** * A proxy class does not compare well. So we first have to get the real * class */ if (rhs == null || Hibernate.getClass(this) != Hibernate.getClass(rhs)) return false; MyClass that = (MyClass ) rhs;
if (!java.util.Objects.equals(fieldname, that.fieldname)) return false; return true; } @Override public int hashCode() { return java.util.Objects.hash(fieldname); } {code} |
|