The platform uses Axelor Set for fields M2M . This requires the implementation of the methods equals and hashCode when 2nd level cache is enabled and it is desired to re-attach a detached entity.
The code generator can automatically generate these two methods. They use all the fields of an entity, which are marked with the attribute single = "true" or hashKey = "true".
So any object that is intended to be used as M2M should have at least one field = "true" or a field marked with hashkey = "true" (multiple fields can also be labeled). If any of these fields are found, the default behavior is used as a backup.
In the case of single entity, attribute hashAll = "true" on the entity allows easy to use all fields (except the fields functions) as a hash key. It is possible to ignore the fields with the attribute hashkey = "false".
Example of use:
<entity name="Circle" hashAll="true">
<string name="code"/>
<string name="name" hashKey="false"/>
</entity>
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (this == obj) return true;
if (!(obj instanceof Circle)) return false;
Circle other = (Circle) obj;
if (this.getId() != null && other.getId() != null) {
return Objects.equal(this.getId(), other.getId());
}
if (!Objects.equal(getCode(), other.getCode())) return false;
return true;
}
@Override
public int hashCode() {
return Objects.hashCode(2018617584, this.getCode());
}