Do we need HyperJAXB generated hashCode & equals methods?

757 Views Asked by At

We've generated some (well a lot) of classes using HyperJAXB. All of the classes implement Equals and HashCode and have the implementation style below. Appears this code is never executed.. is there any particular reason we need this code? I'm looking to simplify the classes if we can.

   public boolean equals(Object object) {
        if (!(object instanceof MyClass)) {
            return false;
        }
        if (this == object) {
            return true;
        }
        final EqualsBuilder equalsBuilder = new JAXBEqualsBuilder();
        equals(object, equalsBuilder);
        return equalsBuilder.isEquals();
    }

    public void hashCode(HashCodeBuilder hashCodeBuilder) {
        hashCodeBuilder.append(this.getValue());
        hashCodeBuilder.append(this.getId());
    }

    public int hashCode() {
        final HashCodeBuilder hashCodeBuilder = new JAXBHashCodeBuilder();
        hashCode(hashCodeBuilder);
        return hashCodeBuilder.toHashCode();
    }
2

There are 2 best solutions below

3
On BEST ANSWER

If your application or the JAXB infrastructure ever puts instances of those objects into collections or maps, then there is a good chance that the equals and hashCode methods will be used.

EDIT

I found the following in the hyperjaxb3 documentation:

Generating equals and hashCode methods

Although this is not directly required by the JPA specification, it is still recommended to have correct implementations of equals and hashCode methods in entity classes. [...] With Hyperjaxb3 you can configure, which equals and hash code builders will be used [...] or turn off equals and hashCode generation off.

So it should be possible to turn off generation of those methods, but the hyperjaxb3 designers seem to be saying that it is better not to.

0
On

You can use

<generateEquals>false</generateEquals>
<generateHashCode>false</generateHashCode>

in your maven-hyperjaxb3-plugin configuration if you want to disable equals/hashCode methods generation. However I won't recommend to.

Sincerely yours, "hyperjaxb3 designers". :)