JPA tree query

KJH·2022년 1월 14일
0

사내에서 개발중인 백엔드 서버의 트리구조 목록 조회 Service 리펙토링(Mybatis to JPA)

진행 중 데이터 조회 부분을 계층형으로 불러 오고 싶어서 찾아본 결과

Oracle의 tree 구조 start with/connect by 처럼 가져오도록 할 수 있다고 한다.

public class ForumCategory {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Where(clause = "deleted='N'")
    @ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
    @JoinColumn(name = "parentId")
    private ForumCategory parent;

    @Where(clause = "deleted='N'")
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "parentId")
    private List<ForumCategory> children = new ArrayList<ForumCategory>();

    @Column(name = "deleted", length = 1, nullable = false)
    private String deleted = "N";

.....
}

출처 : 단일테이블의 Tree 구조를 위한 Self Join

FrontEnd 단에서 d3 stratify()를 사용하므로 채택하진 않음

profile
항상 공부하는 N년차 개발자입니다.

0개의 댓글