Skip to content

Commit

Permalink
๐Ÿ”จ [Refactoring] Lazy fetch type ๋ฆฌํŽ™ํ† ๋ง (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaBeast authored Mar 26, 2024
1 parent 61c6fa5 commit 5f665e6
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,10 +41,11 @@ public PartyPenaltyListAdminResDto findAllPenalty(PageReqDto reqDto) {

Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createdAt"));

Page<PartyPenaltyAdminResDto> penaltyPage = partyPenaltyRepository.findAll(pageable)
.map(PartyPenaltyAdminResDto::new);
Page<PartyPenalty> penaltyPage = partyPenaltyRepository.findUserFetchJoin(pageable);

List<PartyPenaltyAdminResDto> penaltyList = penaltyPage.getContent();
List<PartyPenaltyAdminResDto> penaltyList = penaltyPage.getContent().stream()
.map(PartyPenaltyAdminResDto::new)
.collect(Collectors.toList());

return new PartyPenaltyListAdminResDto(penaltyList, penaltyPage.getTotalPages());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommentReport> commentReportPage = commentReportRepository.findAllWithFetchJoin(pageable);
Page<CommentReport> commentReportPage = commentReportRepository.findAllWithCommentReportFetchJoin(pageable);

List<CommentReportAdminResDto> commentReportPageResDto = commentReportPage.getContent().stream()
.map(CommentReportAdminResDto::new)
Expand All @@ -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<RoomReport> roomReportPage = roomReportRepository.findAllWithFetchJoin(pageable);
Page<RoomReport> roomReportPage = roomReportRepository.findAllWithRoomReportFetchJoin(pageable);

List<RoomReportAdminResDto> roomReportPageResDto = roomReportPage.getContent().stream()
.map(RoomReportAdminResDto::new)
Expand All @@ -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<UserReport> userReportPage = userReportRepository.findAllWithFetchJoin(pageable);
Page<UserReport> userReportPage = userReportRepository.findAllWithUserReportFetchJoin(pageable);

List<UserReportAdminResDto> userReportPageResDto = userReportPage.getContent().stream()
.map(UserReportAdminResDto::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public AdminRoomListResDto findAllRoomList(PageReqDto pageReqDto) {
public AdminRoomDetailResDto findAdminDetailRoom(Long roomId) {
Room room = roomRepository.findById(roomId).orElseThrow(RoomNotFoundException::new);

List<AdminCommentResDto> comments = commentRepository.findByRoomId(roomId).stream()
List<AdminCommentResDto> comments = commentRepository.findAllWithCommentFetchJoin(roomId).stream()
.map(AdminCommentResDto::new)
.collect(Collectors.toList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public RoomDetailResDto findRoomDetail(Long userId, Long roomId) {

if ((room.getStatus() == RoomType.START || room.getStatus() == RoomType.FINISH)
&& userRoomOptional.isPresent()) {
List<CommentResDto> comments = commentRepository.findByRoomId(roomId).stream()
List<CommentResDto> comments = commentRepository.findAllWithCommentFetchJoin(roomId).stream()
.map(comment -> new CommentResDto(comment, comment.getUser().getIntraId()))
.collect(Collectors.toList());

Expand All @@ -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<CommentResDto> comments = commentRepository.findByRoomId(roomId).stream()
List<CommentResDto> comments = commentRepository.findAllWithCommentFetchJoin(roomId).stream()
.map(CommentResDto::new)
.collect(Collectors.toList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public interface CommentReportRepository extends JpaRepository<CommentReport, Lo
+ "JOIN FETCH cr.comment "
+ "JOIN FETCH cr.room",
countQuery = "SELECT count(cr) FROM CommentReport cr")
Page<CommentReport> findAllWithFetchJoin(Pageable pageable);
Page<CommentReport> findAllWithCommentReportFetchJoin(Pageable pageable);
}
11 changes: 11 additions & 0 deletions gg-repo/src/main/java/gg/repo/party/CommentRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Comment, Long> {
List<Comment> 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<Comment> findAllWithCommentFetchJoin(@Param("roomId") Long roomId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +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.PartyPenalty;

public interface PartyPenaltyRepository extends JpaRepository<PartyPenalty, Long> {
PartyPenalty findByUserId(Long id);

List<PartyPenalty> findAllByUserId(Long userId);

@Query(value = "SELECT pp FROM PartyPenalty pp "
+ "JOIN FETCH pp.user ",
countQuery = "SELECT count(pp) FROM PartyPenalty pp")
Page<PartyPenalty> findUserFetchJoin(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public interface RoomReportRepository extends JpaRepository<RoomReport, Long> {
+ "JOIN FETCH rr.reportee "
+ "JOIN FETCH rr.room",
countQuery = "SELECT count(rr) FROM RoomReport rr")
Page<RoomReport> findAllWithFetchJoin(Pageable pageable);
Page<RoomReport> findAllWithRoomReportFetchJoin(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public interface UserReportRepository extends JpaRepository<UserReport, Long> {
+ "JOIN FETCH ur.reportee "
+ "JOIN FETCH ur.room",
countQuery = "SELECT count(ur) FROM UserReport ur")
Page<UserReport> findAllWithFetchJoin(Pageable pageable);
Page<UserReport> findAllWithUserReportFetchJoin(Pageable pageable);
}

0 comments on commit 5f665e6

Please sign in to comment.