Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The Parent entity inside child entity pull the child again

When I try to fetch the child entity, it also gets the parent entity because of one to many bidirectional mapping but the parent entity inside the child again pulling its child. which need to be avoided

Parent entity

@OneToMany(mappedBy = "school", cascade = CascadeType.ALL)
private List<Students> studentsList = new ArrayList<>();

Child Entity

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@ManyToOne(optional = false)
@JoinColumn(name = "school_id",referencedColumnName = "id")
private School school;

While I hit the get call of child entity the response is like

{
    "id": 1,
    "firstName": "firstname",
    "lastName": "lastname",
    "age": "20",
    "school": {
        "id": 1,
        "name": "testName",
        "description": "school",
        "studentsList": [
            1
        ]
    }
}

*But the expected response is

{
    "id": 1,
    "firstName": "firstname",
    "lastName": "lastname",
    "age": "20",
    "school": {
        "id": 1,
        "name": "testName",
        "description": "school"
    }
}

>Solution :

Understood your problem you may use the @JsonIgnoreProperties({}) Annations.

For Example:
In parent Object like this

@OneToMany(mappedBy = "school", cascade = CascadeType.ALL)
@JsonIgnoreProperties({"school"})
private List<Students> studentsList = new ArrayList<>();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading