프로젝트 하던중에 게시판을 만들어야 했다
post 테이블과 postComment 테이블이 있고
Comment 는 다음과 같다
@Entity
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "post_comment")
public class PostComment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private Long userId;
@Column(nullable = false)
private Long postId;
@Column(nullable = false)
private String content;
@Column()
private Long replyCommentId;
@Column(nullable = false)
private Boolean deleted;
@Column(nullable = false)
private LocalDateTime createdDt;
public static PostComment of() {
return PostComment.builder()
.deleted(true)
.createdDt(LocalDateTime.now())
.build();
}
}
댓글 그리고 대댓글만 제공하기로 하였고
replyCommentId는 값이 없으면 부모댓글(depth=0)
값이 있으면 자식댓글(depth=1)이다
이때 애브리타임이나 당근마켓처럼...
더보기를 눌렀을 때 또는 스크롤을 내릴 때 요청이 발생해야하는데
댓글+대댓글을 한 테이블에서 관리하면서
단 한번의 쿼리에 댓글+대댓글의 원하는 size(limit)를 어떻게 구하나 고민을 많이 했었다..
근데 프로젝트 같이하시는 분의 도움으로 해당 방법?.. 을 알게 되어서 글을 쓰게 되었따
결론만 말하면 postComment 끼리 innerJoin 하면 되는데
where에는 replyCommentId 컬럼이 null인 것(A)과
replyCommentId 컬럼이 존재하는(B)인 것을 묶어주면된다.
offset이 2개 존재한다
두 테이블을 곱했을 때(join했을 때)
기존 테이블의 id를 primaryOffset
옆에 딸려오는 replyId를 subOffset으로 뒀다
이로써 원하는 limit만큼 대댓글을 포함한 댓글들을 조회할 수 있게 됐따