JPA Bidirectional relationship - infinite loop / circular reference

与世无争的帅哥 提交于 2020-01-03 15:56:24

问题


I have a bidirectional relationship

@Entity
@Table(name = "facility")
public class Facility implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(mappedBy = "facility")
    private Set<Amenity> amenities;
}

@Entity
@Table(name = "amenity")
public class Amenity  implements Serializable {
    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne
    private Facility facility;
}

This all works fine and I can see the table is created correctly. I can add data fine through a rest endpoint and but it was only when I wanted to go in the opposite direct and get the facility for an amenity when I got this issue. Its look like its going into an infinite loop, facility calls amenity calls facility calls amenity and so on. Its like there is some circular reference. I've have searched high and low , far and wide and followed many examples and all seem to have entities set up in a similar fashion. I have left out getters and setters for brevity. I would expect after it got its first amenity it would stop.

[
    {
        "id": 1,
        "facilityName": "asdf",
        "facilityCode": "asdf",
        "amenities": [
            {
                "id": 15,
                "amenityType": "amenity 1",
                "facility": {
                    "id": 1,
                    "facilityName": "asdf",
                    "facilityCode": "asdf",
                    "amenities": [
                        {

来源:https://stackoverflow.com/questions/30423259/jpa-bidirectional-relationship-infinite-loop-circular-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!