子实体中的父实体再次拉动子实体

The Parent entity inside child entity pull the child again

当我尝试获取子实体时,由于一对多双向映射,它也获取了父实体,但是子实体中的父实体再次拉动了它的子实体。需要避免

父实体

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

子实体

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

当我点击子实体的 get 调用时,响应就像

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

*但预期的响应是

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

使用“Eager”获取。

@OneToMany(fetch = FetchType.EAGER, mappedBy = "school", cascade = CascadeType.ALL)
private List<Students> studentsList = new ArrayList<>();
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<>();