SOLUX-우리만 앱

JPA 순환 참조 해결

leeeehhjj 2021. 8. 8. 17:05

다대일 양방향 관계를 구현할 때 무한 순환 참조가 발생했다.

 

@ManyToOne
    @JoinColumn(name = "Club_Id")
    private Club club;

Notice에서 위와 같이 ManyToOne으로 club을 매핑하고,

@OneToMany (mappedBy = "club")
    private List<Notice> notices = new ArrayList<Notice>();

Club에서 위와 같이 oneToMany로 notice를 조회할 수 있도록 했다.

 

이렇게 한 후 notice를 조회하면 아래처럼 notice를 통해 club을 참조하고, club을 통해 notice를 참조하고, 이 과정을 무한 반복하게 된다. 

{
    "id": 1,
    "title": "djjl",
    "content": "de33e",
    "author": null,
    "club": {
        "id": 1,
        "name": "dljff",
        "generation": 3,
        "info": null,
        "clubCode": "dliejl;adlj",
        "notices": [
            {
                "createdDate": "2021-08-08T16:50:03.039556",
                "modifiedDate": "2021-08-08T16:50:03.039556",
                "id": 1,
                "title": "djjl",
                "content": "de33e",
                "author": null,
                "club": {
                    "id": 1,
                    "name": "dljff",
                    "generation": 3,
                    "info": null,
                    "clubCode": "dliejl;adlj",
                    "notices": [
                        {
                            "createdDate": "2021-08-08T16:50:03.039556",
                            "modifiedDate": "2021-08-08T16:50:03.039556",
                            "id": 1,
                            "title": "djjl",
                            "content": "de33e",
                            "author": null,
                            "club": {
                                "id": 1,
                                "name": "dljff",
                                "generation": 3,
                                "info": null,
                                "clubCode": "dliejl;adlj",
...

 

이 문제를 해결하는 방법은 2가지가 있다.

1. @JsonIgnore

2. @JsonBackReference, @JsonManagedReference

 

1. @JsonIgnore은 이름에서 보이듯이 아예 json으로 보여주지 않는 방법이다.

@JsonIgnore
    @ManyToOne
    @JoinColumn(name = "Club_Id")
    private Club club;

예를 들어 이렇게 작성하면 notice를 조회할 때 club에 대한 내용은 보여주지 않는 것이다.

 

2. @JsonBackReference는 직렬화되지 않도록 하는 것이고, @JsonManagedReference는 정상적으로 직렬화되도록 하는 것이다.

@JsonBackReference
    @ManyToOne
    @JoinColumn(name = "Club_Id")
    private Club club;

즉, notice는 위와 같이 jsonBackReference로 설정하고,

@JsonManagedReference
    @OneToMany (mappedBy = "club")
    private List<Notice> notices = new ArrayList<Notice>();

club은 @JsonManagedReference로 설정하면

{
    "id": 1,
    "title": "djjl",
    "content": "de33e",
    "author": null,
    "club": {
        "id": 1,
        "name": "dljff",
        "generation": 3,
        "info": null,
        "clubCode": "dliejl;adlj",
        "notices": [
            {
                "createdDate": "2021-08-08T17:00:54.62225",
                "modifiedDate": "2021-08-08T17:00:54.62225",
                "id": 1,
                "title": "djjl",
                "content": "de33e",
                "author": null
            }
        ],
        "posts": [],
        "receipts": []
    },
    "createdDate": "2021-08-08T17:00:54.62225",
    "modifiedDate": "2021-08-08T17:00:54.62225"
}

이와 같이 무한 참조를 하지 않는 결과를 볼 수 있다.