Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JPA Entity check requires all fields to be used in hashCode #853

Closed
sithmein opened this issue Sep 11, 2023 · 3 comments
Closed

JPA Entity check requires all fields to be used in hashCode #853

sithmein opened this issue Sep 11, 2023 · 3 comments
Labels

Comments

@sithmein
Copy link

We have JPA entities which don't use all properties in the hashCode implementation. Instead we rely on the unique ID only. Therefore we already had enabled suppress(Warning.STRICT_HASHCODE) in the past. However, the new JPA entity check seems to require that all properties are used in the hashCode implementation. If a property is not used you get a (false) error such as

-> JPA Entity: direct reference to field links used in hashCode instead of getter getLinks.

In fact neither the field nor the getter are used (on purpose).

The following code shows the problem:

import java.util.Collection;
import java.util.Objects;

import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.Test;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.OneToMany;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;

class JPATest {
    @Entity
    public static class Ent {
        @Column(name = "id")
        String id;

        @OneToMany(fetch = FetchType.LAZY)
        Collection<String> links;

        public String getId() {
            return id;
        }

        public Collection<String> getLinks() {
            return links;
        }

        @Override
        public int hashCode() {
            return id.hashCode(); // id only on purpose
        }

        @Override
        public boolean equals(final @Nullable Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            Ent other = (Ent)obj;
            return Objects.equals(id, other.id) && Objects.equals(getLinks(), other.getLinks());
        }
    }

    @Test
    void test() {
        EqualsVerifier.forClass(Ent.class)
            .usingGetClass()
            .suppress(Warning.STRICT_HASHCODE)
            .verify();
    }
}

The expected behaviour is that this test passes but instead it fails with the above error message.

I tested with version 3.15.1.

@jqno
Copy link
Owner

jqno commented Sep 13, 2023

I've reproduced the problem, I'll try to think of a way to solve it. Will let you know when I do.

@jqno jqno added the accepted label Sep 13, 2023
@jqno
Copy link
Owner

jqno commented Sep 23, 2023

I've just released version 3.15.2 with a fix for this problem!

@jqno jqno closed this as completed Sep 23, 2023
@sithmein
Copy link
Author

sithmein commented Oct 9, 2023

I finally came around to try the new version and the problem is now solved. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants