Persistence Framework/ORM -JPA

JPA 양방향 순환참조

prden 2023. 2. 9. 21:10

@JsonIgnore || DTO

@Entity
@Table(name = "`employee`")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Employee {

    @Id
    @Column(name = "employee_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long employeeId;

    @Column(name = "name")
    private String name;

    @Column(name = "phone_number")
    private String phoneNumber;

    @Column(name = "registration_num")
    private String registrationNum;

    @Column(name = "company_name")
    private String companyName;

    @Column(name = "position")
    private String position;

    @Column(name = "grade")
    private String grade;

    @Column(name = "employee_rank")
    private String rank;

    @Column(name = "description")
    private String description;

    @Column(name = "abnormality")
    private String abnormality;

    @Column(name = "gender")
    private String gender;

    @Column(name = "employee_type")
    private String employeeType;

    @Column(name = "email")
    private String email;

    @OneToMany(mappedBy = "employee")
    @JsonIgnore //를 통해 제거
    private List<Project> projects = new ArrayList<>();

    @OneToMany(mappedBy = "employee")
    @JsonIgnore //를 통해 제거
    private List<CompanyHistory> companyHistories = new ArrayList<>();

    @OneToMany(mappedBy = "employee")
    @JsonIgnore //를 통해 제거
    private List<Certificate> certificates = new ArrayList<>();

    @OneToMany(mappedBy = "employee")
    @JsonIgnore //를 통해 제거
    private List<Education> educations = new ArrayList<>();

    @OneToMany(mappedBy = "employee")
    @JsonIgnore //를 통해 제거
    private List<Contract> contracts = new ArrayList<>();

    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "address", column = @Column(name = "address")),
            @AttributeOverride(name = "addressDetails", column = @Column(name = "address_details")),
            @AttributeOverride(name = "zipCode", column = @Column(name = "zip_code"))
    })
    private Address address;

    @Builder
    public Employee(String name, String phoneNumber, String registrationNum, String companyName, String position, String grade, String rank, String description, String abnormality, String gender, String employeeType, String email, List<Project> projects, List<CompanyHistory> companyHistories, List<Certificate> certificates, List<Education> educations, List<Contract> contracts, Address address) {
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.registrationNum = registrationNum;
        this.companyName = companyName;
        this.position = position;
        this.grade = grade;
        this.rank = rank;
        this.description = description;
        this.abnormality = abnormality;
        this.gender = gender;
        this.employeeType = employeeType;
        this.email = email;
        this.projects = projects;
        this.companyHistories = companyHistories;
        this.certificates = certificates;
        this.educations = educations;
        this.contracts = contracts;
        this.address = address;
    }
    
    // DTO를 통해 원하는 값만
      public EmployeeDto.EmployeeDetailResponse toResponseDto() {
        return EmployeeDto.EmployeeDetailResponse.builder()
                .name(name)
                .phoneNumber(phoneNumber)
                .registrationNum(registrationNum)
                .companyName(companyName)
                .position(position)
                .grade(grade)
                .rank(rank)
                .description(description)
                .abnormality(abnormality)
                .gender(gender)
                .employeeType(employeeType)
                .email(email)
                .build();
    }
}

1. @JsonIgnore를 통해 해결

해당 프로퍼티에 @JsonIgnore를 붙여서  return에 포함되지 않도록

 

2. DTO를 통해 해결

Entity 자체를 ruturn하지 않고 DTO 객체를 만들어 필요한 데이터만 return한다.

 

3. 양방향 매핑이 필요하지 않으면 단방향매핑으로 변경

https://dev-coco.tistory.com/133

 

[JPA] 양방향 순환참조 문제 및 해결방법

게시판 프로젝트를 진행하던 중 순환 참조 문제에 마주하였다. ※ 순환 참조(Circular reference)란, 참조하는 대상이 서로 물려 있어 참조할 수 없게 되는 현상을 말한다. JPA에서 양방향으로 연결된 E

dev-coco.tistory.com

 

 

'Persistence Framework > ORM -JPA' 카테고리의 다른 글

JPA Insert vs. Update  (0) 2023.01.20
Spring Data JPA Pagination, Querydsl의 pagenation연결  (0) 2022.10.11
QueryDsl, 사용자 정의 Repository  (0) 2022.10.11
QueryDsl 설정  (0) 2022.10.05
N+1문제  (0) 2021.08.30