Is the below is correct equals and hashCode implementation in Java? -
when object's value equal need return true.
example
@override public int hashcode() { return new hashcodebuilder().append(value).tohashcode(); } @override public boolean equals(final object obj) { if (obj instanceof numbervalto) { final numbervalto other = (numberval) obj; return new equalsbuilder().append(value, other.getvalue()).isequals(); } return false; }
is above fine or wrong?
i saw in few applications hashcode being multiple each , every field of table , not sure whether correct approach.
assume entity has 4 columns assume entity has 2 columns
- which best approach generate same?
- also, need implement hashcode() , equals() hibernate entity class?
thanks.
yes it's fine, assuming apache commons helper classes you're using. @vino correct point out adding if (obj == this) return true;
start of equals
method can worthwhile optimisation, methods correct is.
Comments
Post a Comment