From 5f665e6cfd1214e81905cc4b6b36c5fee6fe7d4c Mon Sep 17 00:00:00 2001 From: jungjkim Date: Tue, 26 Mar 2024 21:24:55 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20[Refactoring]=20Lazy=20fetch=20t?= =?UTF-8?q?ype=20=EB=A6=AC=ED=8E=99=ED=86=A0=EB=A7=81=20(#772)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../penalty/service/PartyPenaltyAdminService.java | 8 +++++--- .../api/admin/report/service/ReportAdminService.java | 6 +++--- .../api/admin/room/service/RoomAdminService.java | 2 +- .../party/api/user/room/service/RoomFindService.java | 4 ++-- .../java/gg/repo/party/CommentReportRepository.java | 2 +- .../main/java/gg/repo/party/CommentRepository.java | 11 +++++++++++ .../java/gg/repo/party/PartyPenaltyRepository.java | 9 +++++++++ .../main/java/gg/repo/party/RoomReportRepository.java | 2 +- .../main/java/gg/repo/party/UserReportRepository.java | 2 +- 9 files changed, 34 insertions(+), 12 deletions(-) diff --git a/gg-pingpong-api/src/main/java/gg/party/api/admin/penalty/service/PartyPenaltyAdminService.java b/gg-pingpong-api/src/main/java/gg/party/api/admin/penalty/service/PartyPenaltyAdminService.java index 62b707897..19c389b8f 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/admin/penalty/service/PartyPenaltyAdminService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/admin/penalty/service/PartyPenaltyAdminService.java @@ -2,6 +2,7 @@ import java.time.LocalDateTime; import java.util.List; +import java.util.stream.Collectors; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -40,10 +41,11 @@ public PartyPenaltyListAdminResDto findAllPenalty(PageReqDto reqDto) { Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createdAt")); - Page penaltyPage = partyPenaltyRepository.findAll(pageable) - .map(PartyPenaltyAdminResDto::new); + Page penaltyPage = partyPenaltyRepository.findUserFetchJoin(pageable); - List penaltyList = penaltyPage.getContent(); + List penaltyList = penaltyPage.getContent().stream() + .map(PartyPenaltyAdminResDto::new) + .collect(Collectors.toList()); return new PartyPenaltyListAdminResDto(penaltyList, penaltyPage.getTotalPages()); } diff --git a/gg-pingpong-api/src/main/java/gg/party/api/admin/report/service/ReportAdminService.java b/gg-pingpong-api/src/main/java/gg/party/api/admin/report/service/ReportAdminService.java index f58bed2c0..3eb7b4420 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/admin/report/service/ReportAdminService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/admin/report/service/ReportAdminService.java @@ -42,7 +42,7 @@ public CommentReportListResDto getCommentReports(ReportPageReqDto reportPageReqD int size = reportPageReqDto.getSize(); Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createdAt")); - Page commentReportPage = commentReportRepository.findAllWithFetchJoin(pageable); + Page commentReportPage = commentReportRepository.findAllWithCommentReportFetchJoin(pageable); List commentReportPageResDto = commentReportPage.getContent().stream() .map(CommentReportAdminResDto::new) @@ -61,7 +61,7 @@ public RoomReportListResDto getRoomReports(ReportPageReqDto reportPageReqDto) { int size = reportPageReqDto.getSize(); Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createdAt")); - Page roomReportPage = roomReportRepository.findAllWithFetchJoin(pageable); + Page roomReportPage = roomReportRepository.findAllWithRoomReportFetchJoin(pageable); List roomReportPageResDto = roomReportPage.getContent().stream() .map(RoomReportAdminResDto::new) @@ -80,7 +80,7 @@ public UserReportListResDto getUserReports(ReportPageReqDto reportPageReqDto) { int size = reportPageReqDto.getSize(); Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createdAt")); - Page userReportPage = userReportRepository.findAllWithFetchJoin(pageable); + Page userReportPage = userReportRepository.findAllWithUserReportFetchJoin(pageable); List userReportPageResDto = userReportPage.getContent().stream() .map(UserReportAdminResDto::new) diff --git a/gg-pingpong-api/src/main/java/gg/party/api/admin/room/service/RoomAdminService.java b/gg-pingpong-api/src/main/java/gg/party/api/admin/room/service/RoomAdminService.java index 73be1256c..c6dd38a90 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/admin/room/service/RoomAdminService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/admin/room/service/RoomAdminService.java @@ -85,7 +85,7 @@ public AdminRoomListResDto findAllRoomList(PageReqDto pageReqDto) { public AdminRoomDetailResDto findAdminDetailRoom(Long roomId) { Room room = roomRepository.findById(roomId).orElseThrow(RoomNotFoundException::new); - List comments = commentRepository.findByRoomId(roomId).stream() + List comments = commentRepository.findAllWithCommentFetchJoin(roomId).stream() .map(AdminCommentResDto::new) .collect(Collectors.toList()); diff --git a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomFindService.java b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomFindService.java index af14fd5f5..aa32d872a 100644 --- a/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomFindService.java +++ b/gg-pingpong-api/src/main/java/gg/party/api/user/room/service/RoomFindService.java @@ -135,7 +135,7 @@ public RoomDetailResDto findRoomDetail(Long userId, Long roomId) { if ((room.getStatus() == RoomType.START || room.getStatus() == RoomType.FINISH) && userRoomOptional.isPresent()) { - List comments = commentRepository.findByRoomId(roomId).stream() + List comments = commentRepository.findAllWithCommentFetchJoin(roomId).stream() .map(comment -> new CommentResDto(comment, comment.getUser().getIntraId())) .collect(Collectors.toList()); @@ -146,7 +146,7 @@ public RoomDetailResDto findRoomDetail(Long userId, Long roomId) { .collect(Collectors.toList()); return new RoomDetailResDto(room, myNickname, hostNickname, roomUsers, comments); } else { // if 참여자 && Start or Finish 상태인 경우 intraID || else intraId == null - List comments = commentRepository.findByRoomId(roomId).stream() + List comments = commentRepository.findAllWithCommentFetchJoin(roomId).stream() .map(CommentResDto::new) .collect(Collectors.toList()); diff --git a/gg-repo/src/main/java/gg/repo/party/CommentReportRepository.java b/gg-repo/src/main/java/gg/repo/party/CommentReportRepository.java index 3f821f2d7..725349f94 100644 --- a/gg-repo/src/main/java/gg/repo/party/CommentReportRepository.java +++ b/gg-repo/src/main/java/gg/repo/party/CommentReportRepository.java @@ -21,5 +21,5 @@ public interface CommentReportRepository extends JpaRepository findAllWithFetchJoin(Pageable pageable); + Page findAllWithCommentReportFetchJoin(Pageable pageable); } diff --git a/gg-repo/src/main/java/gg/repo/party/CommentRepository.java b/gg-repo/src/main/java/gg/repo/party/CommentRepository.java index 97e16d251..e4a50cb91 100644 --- a/gg-repo/src/main/java/gg/repo/party/CommentRepository.java +++ b/gg-repo/src/main/java/gg/repo/party/CommentRepository.java @@ -2,10 +2,21 @@ import java.util.List; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import gg.data.party.Comment; public interface CommentRepository extends JpaRepository { List findByRoomId(Long roomId); + + @Query("SELECT c FROM Comment c " + + "JOIN FETCH c.user " + + "JOIN FETCH c.userRoom " + + "JOIN FETCH c.room r " + + "WHERE r.id = :roomId") + List findAllWithCommentFetchJoin(@Param("roomId") Long roomId); } diff --git a/gg-repo/src/main/java/gg/repo/party/PartyPenaltyRepository.java b/gg-repo/src/main/java/gg/repo/party/PartyPenaltyRepository.java index 0b2f23e09..fc8d27efe 100644 --- a/gg-repo/src/main/java/gg/repo/party/PartyPenaltyRepository.java +++ b/gg-repo/src/main/java/gg/repo/party/PartyPenaltyRepository.java @@ -2,7 +2,11 @@ import java.util.List; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import gg.data.party.PartyPenalty; @@ -10,4 +14,9 @@ public interface PartyPenaltyRepository extends JpaRepository findAllByUserId(Long userId); + + @Query(value = "SELECT pp FROM PartyPenalty pp " + + "JOIN FETCH pp.user ", + countQuery = "SELECT count(pp) FROM PartyPenalty pp") + Page findUserFetchJoin(Pageable pageable); } diff --git a/gg-repo/src/main/java/gg/repo/party/RoomReportRepository.java b/gg-repo/src/main/java/gg/repo/party/RoomReportRepository.java index c74c15421..3953edd76 100644 --- a/gg-repo/src/main/java/gg/repo/party/RoomReportRepository.java +++ b/gg-repo/src/main/java/gg/repo/party/RoomReportRepository.java @@ -23,5 +23,5 @@ public interface RoomReportRepository extends JpaRepository { + "JOIN FETCH rr.reportee " + "JOIN FETCH rr.room", countQuery = "SELECT count(rr) FROM RoomReport rr") - Page findAllWithFetchJoin(Pageable pageable); + Page findAllWithRoomReportFetchJoin(Pageable pageable); } diff --git a/gg-repo/src/main/java/gg/repo/party/UserReportRepository.java b/gg-repo/src/main/java/gg/repo/party/UserReportRepository.java index eee65f8bc..e8ce2a80e 100644 --- a/gg-repo/src/main/java/gg/repo/party/UserReportRepository.java +++ b/gg-repo/src/main/java/gg/repo/party/UserReportRepository.java @@ -24,5 +24,5 @@ public interface UserReportRepository extends JpaRepository { + "JOIN FETCH ur.reportee " + "JOIN FETCH ur.room", countQuery = "SELECT count(ur) FROM UserReport ur") - Page findAllWithFetchJoin(Pageable pageable); + Page findAllWithUserReportFetchJoin(Pageable pageable); }