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 fails to validate final entity class with @GeneratedValue id #929

Closed
EagerSloth opened this issue Feb 29, 2024 · 4 comments
Closed

Comments

@EagerSloth
Copy link

EagerSloth commented Feb 29, 2024

Describe the bug
Since version 3.15.5 equalsVerifier fails to validate one of my entity classes.

Code that triggers the behavior
Entity class:

@Entity
public final class JpaEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        JpaEntity that = (JpaEntity) o;
        return getId() != null && Objects.equals(getId(), that.getId());
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}

test case:

    @Test
    void equals() {
        EqualsVerifier.forClass(JpaEntity.class)
                .suppress(Warning.IDENTICAL_COPY_FOR_VERSIONED_ENTITY)
                .suppress(Warning.STRICT_HASHCODE)
                .suppress(Warning.SURROGATE_KEY)
                .verify();
    }

Remember, you have the problem right in front of you. I have to try and reproduce it in my spare time. Please help me, so that I can help you!

Error message
java.lang.AssertionError: EqualsVerifier found a problem in class com.whitewall.integrationapi.order.domain.JpaEntity.
-> Cannot subclass primitive, array or final types: class com.whitewall.integrationapi.order.domain.JpaEntity

Expected behavior
The test should still pass as in prior version - or give a hint how to fix the test.
The exception disappears when also suppressing Warning.JPA_GETTER - but I am not sure about the implications.

Version
Test OK in 3.15.4. fails in 3.15.5 - 3.15.7

@jqno
Copy link
Owner

jqno commented Mar 1, 2024

Found the problem. EqualsVerifier considers a field marked with @GeneratedValue to be lazy, and applies its lazy field checks for that field. In that case, it assumes the entity is non-final (because Hibernate would also create a subclass to handle lazy loading of those fields). Your entity, however, is final. I didn't know that was allowed when there's a field with @GeneratedValue, but I'll just assume that it is, since you're obviously using that 😄

I've found a fix, too. I'll make a release later today.

@jqno
Copy link
Owner

jqno commented Mar 1, 2024

Version 3.15.8 is syncing with Maven Central now, it should fix your problem!

@jqno jqno closed this as completed Mar 1, 2024
@EagerSloth
Copy link
Author

Thank you for the quick fix - confirming this solves our problem.

@jqno
Copy link
Owner

jqno commented Mar 1, 2024

Awesome, glad to hear it!

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