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

Fix wrong otherEntityName in relationship setter #15299

Merged

Conversation

iconben
Copy link
Contributor

@iconben iconben commented Jun 11, 2021

I ran into this bug in one of my project, generated code is:

    @JsonIgnoreProperties(value = { "student" }, allowSetters = true)
    @OneToOne(mappedBy = "student")
    private StudentForeignerInfo foreignerInfo;

    ......

    public void setForeignerInfo(StudentForeignerInfo studentForeignerInfo) {
        if (this.foreignerInfo != null) {
            this.foreignerInfo.setStudent(null);
        }
        if (foreignerInfo != null) {
            foreignerInfo.setStudent(this);
        }
        this.foreignerInfo = studentForeignerInfo;
    }

should be:

    @JsonIgnoreProperties(value = { "student" }, allowSetters = true)
    @OneToOne(mappedBy = "student")
    private StudentForeignerInfo foreignerInfo;

    ......

    public void setForeignerInfo(StudentForeignerInfo studentForeignerInfo) {
        if (this.foreignerInfo != null) {
            this.foreignerInfo.setStudent(null);
        }
        if (studentForeignerInfo != null) {         // have to fix here
            studentForeignerInfo.setStudent(this);   // and here
        }
        this.foreignerInfo = studentForeignerInfo;
    }

In default case where name of $otherEntityName equals to name of $relationshipFieldName, the setter works well.

However if name of $otherEntityName is customized, '$relationshipFieldName' will actually be explained as 'this.$relationshipFieldName' which was just set parent to null at the preceding line but again set back to 'this', this logic is apparently wrong.


Please make sure the below checklist is followed for Pull Requests.

When you are still working on the PR, consider converting it to Draft (bellow reviewers) and adding skip-ci label, you can still see CI build result at your branch.

@iconben iconben changed the title fix wrong otherEntityName in relationship setter Fix wrong otherEntityName in relationship setter Jun 11, 2021
Copy link
Member

@mshima mshima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mshima mshima merged commit 0f42fb5 into jhipster:main Jun 15, 2021
@pascalgrimaud pascalgrimaud added this to the 7.1.0 milestone Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants