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

The implementation of the equals method is not Hibernate-safe #23753

Closed
1 task done
jperezdelafuente opened this issue Oct 5, 2023 · 2 comments · Fixed by #23839
Closed
1 task done

The implementation of the equals method is not Hibernate-safe #23753

jperezdelafuente opened this issue Oct 5, 2023 · 2 comments · Fixed by #23839

Comments

@jperezdelafuente
Copy link
Contributor

Overview of the issue

Jhipster generates the equals methods like this:

@Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Foo)) {
            return false;
        }
        return id != null && id.equals(((Foo) o).id);
    }
Motivation for or Use Case

This implementation is problematic with entities wrapped in hibernate proxies:
https://blog.andrewbeacock.com/2008/08/how-to-implement-hibernate-safe-equals.html

In the hasCode() methods generated by Jhipster it appears

@Override
    public int hashCode() {
        // see https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
        return getClass().hashCode();
    }

but that implementation:
https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
is not respected with getId in the equals methods generated by jhipster

Suggest a Fix

A correct implementation for hibernate proxies would be (with "getId()" instead of "id"):

@Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Foo)) {
            return false;
        }
        return id != null && id.equals(((Foo) o).getId());
    }
JHipster Version(s)

Jhipster 8.0.0 - beta 3

  • Checking this box is mandatory (this is just to show you read everything)
@mshima
Copy link
Member

mshima commented Oct 9, 2023

@jperezdelafuente can you contribute with a PR?

@vandenn3
Copy link

I also got a bug because of this implementation issue in generated equals. Thanks for creating this issue!

I see however that this change was already done in the past in #5604 , but then changed back here 1973fc6#diff-b2f9dbf1a993c204d3446d607494894a72f73dfb38228358c8e97b4d9d13da1c related to #8656 . I'm not sure that the rollback was intentional, as the goal of the fix for the issue 8656 was to allow comparison with subclasses (so getClass() was replaced by instanceof). I don't see any reason that would explain why the getters were removed in the fix of issue 8656.

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

Successfully merging a pull request may close this issue.

4 participants