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

EqualsVerifier ignores Warning.SURROGATE_KEY #763

Closed
sburkard opened this issue Feb 8, 2023 · 2 comments
Closed

EqualsVerifier ignores Warning.SURROGATE_KEY #763

sburkard opened this issue Feb 8, 2023 · 2 comments

Comments

@sburkard
Copy link

sburkard commented Feb 8, 2023

Describe the bug
Abstract Superclass for all JPA-Entities

@MappedSuperclass
public abstract class AbstractEntity implements Serializable {
    @Id
    @Column(unique = true, updatable = false, nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    protected Long id;

    // getters/setters/constructors removed for brevity

    @Override
    public final boolean equals(Object o) {   // final as suggested on https://jqno.nl/equalsverifier/manual/jpa-entities
        // according to https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/ 
        if (this == o) return true;
        if (!(o instanceof AbstractEntity)) return false;
        AbstractEntity other = (AbstractEntity) o;
        return id != null && Objects.equals(getId(), other.getId());
    }
    @Override
    public final int hashCode() {    // final as suggested on https://jqno.nl/equalsverifier/manual/jpa-entities
        // according to https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/ 
        return getClass().hashCode();
    }
}

Example JPA-Entity as Subclass

@Entity
public class Person extends AbstractEntity {
    private String name;
    // getters/setters/constructors removed for brevity
}

To Reproduce

First try with Warning.SURROGATE_KEY because we have one

@Test
void givenJpaEntityPerson_whenTestEqualsHashcode_thenRespectJpaContract() {
    EqualsVerifier.simple().forClass(Person.class).suppress(Warning.SURROGATE_KEY).verify();
}

Message: Reflexivity: object does not equal an identical copy of itself:
If this is intentional, consider suppressing Warning.IDENTICAL_COPY

Yes, this is intentional, therefore

@Test
void givenJpaEntityPerson_whenTestEqualsHashcode_thenRespectJpaContract() {
    EqualsVerifier.simple().forClass(Person.class)
        .suppress(Warning.SURROGATE_KEY)
        .suppress(Warning.IDENTICAL_COPY_FOR_VERSIONED_ENTITY).verify();
}

Message: Significant fields: equals does not use name, or it is stateless.

This is not as expected because I have an @id field and I say "suppress(Warning.SURROGATE_KEY)".
Nevertheless, complains EqualsVerifier that "name" is not used for equality.

Code that triggers the behavior
See above

Error message
See above

Expected behavior
I expected that EqualsVerifier ignores all Entity fields except @id field

Version
3.13

Additional context

@jqno
Copy link
Owner

jqno commented Feb 10, 2023

I've just released version 3.13.1 which fixes this!

@jqno jqno closed this as completed Feb 10, 2023
@sburkard
Copy link
Author

Works as expected, thanks a lot

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

No branches or pull requests

2 participants