diff --git a/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepository.java b/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepository.java new file mode 100644 index 00000000..9993d75c --- /dev/null +++ b/server/src/main/java/com/main36/pikcha/domain/comment/repository/CommentCustomRepository.java @@ -0,0 +1,27 @@ +package com.main36.pikcha.domain.comment.repository; + +import com.main36.pikcha.domain.comment.entity.Comment; +import com.main36.pikcha.domain.post.entity.Post; +import com.querydsl.jpa.impl.JPAQueryFactory; +import org.springframework.stereotype.Repository; +import java.util.List; + +import static com.main36.pikcha.domain.comment.entity.QComment.comment; + +@Repository +public class CommentCustomRepository { + private JPAQueryFactory jpaQueryFactory; + + public CommentCustomRepository(JPAQueryFactory jpaQueryFactory) { + this.jpaQueryFactory = jpaQueryFactory; + } + + public List findCommentByPost(Post post){ + return jpaQueryFactory.selectFrom(comment) + .leftJoin(comment.parent) + .fetchJoin() + .where(comment.post.postId.eq(post.getPostId())) + .orderBy(comment.parent.commentId.asc().nullsFirst(), comment.createdAt.asc()) + .fetch(); + } +} diff --git a/server/src/main/java/com/main36/pikcha/global/config/QueryDslConfig.java b/server/src/main/java/com/main36/pikcha/global/config/QueryDslConfig.java new file mode 100644 index 00000000..5f21de88 --- /dev/null +++ b/server/src/main/java/com/main36/pikcha/global/config/QueryDslConfig.java @@ -0,0 +1,19 @@ +package com.main36.pikcha.global.config; + +import com.querydsl.jpa.impl.JPAQueryFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +@Configuration +public class QueryDslConfig { + @PersistenceContext + private EntityManager entityManager; + + @Bean + public JPAQueryFactory jpaQueryFactory(){ + return new JPAQueryFactory(entityManager); + } +}