Skip to content

Commit

Permalink
[BYOB-232] 구매 노트 및 감상 노트 목록 조회 시 발생하는 N+1 문제 해결 (#75)
Browse files Browse the repository at this point in the history
* fix: Note에 연관된 Liquor를 v2로 변경

* fix: MultipleBagFetchException을 피해서 N+1 문제를 해결하기 위해 BatchSize 적용
  • Loading branch information
y-ngm-n authored Nov 15, 2024
1 parent c292366 commit a0616f0
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package team_alcoholic.jumo_server.v2.liquor.dto;

import lombok.Getter;
import lombok.Setter;
import team_alcoholic.jumo_server.v2.liquor.domain.NewLiquor;

/**
*
*/
@Getter @Setter
public class LiquorSimpleRes {
private Long id;
private String koName;
private String enName;
private String thumbnailImageUrl;
private String type;
private String abv;
// private LiquorCategoryRes category;

public static LiquorSimpleRes from(NewLiquor liquor) {
LiquorSimpleRes dto = new LiquorSimpleRes();
dto.setId(liquor.getId());
dto.setKoName(liquor.getKoName());
dto.setEnName(liquor.getEnName());
dto.setThumbnailImageUrl(liquor.getThumbnailImageUrl());
dto.setType(liquor.getType());
dto.setAbv(liquor.getAbv());

return dto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import jakarta.persistence.*;
import lombok.Getter;
import team_alcoholic.jumo_server.v1.liquor.domain.Liquor;
import org.hibernate.annotations.BatchSize;
import team_alcoholic.jumo_server.global.common.domain.BaseTimeEntity;
import team_alcoholic.jumo_server.v2.liquor.domain.NewLiquor;
import team_alcoholic.jumo_server.v2.user.domain.NewUser;

import java.util.ArrayList;
Expand All @@ -25,13 +26,14 @@ public abstract class Note extends BaseTimeEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "liquor_id")
private Liquor liquor;
private NewLiquor liquor;

@OneToMany(mappedBy = "note", fetch = FetchType.LAZY)
@BatchSize(size = 100)
private List<NoteImage> noteImages = new ArrayList<>();

protected Note() {}
public Note(NewUser user, Liquor liquor) {
public Note(NewUser user, NewLiquor liquor) {
this.user = user;
this.liquor = liquor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.persistence.Entity;
import lombok.Getter;
import team_alcoholic.jumo_server.v1.liquor.domain.Liquor;
import team_alcoholic.jumo_server.v2.liquor.domain.NewLiquor;
import team_alcoholic.jumo_server.v2.note.dto.request.PurchaseNoteCreateReq;
import team_alcoholic.jumo_server.v2.note.dto.request.PurchaseNoteUpdateReq;
import team_alcoholic.jumo_server.v2.user.domain.NewUser;
Expand All @@ -22,7 +23,7 @@ public class PurchaseNote extends Note {
private String content;

protected PurchaseNote() {}
public PurchaseNote(PurchaseNoteCreateReq noteCreateReq, NewUser user, Liquor liquor) {
public PurchaseNote(PurchaseNoteCreateReq noteCreateReq, NewUser user, NewLiquor liquor) {
super(user, liquor);
this.purchaseAt = noteCreateReq.getPurchaseAt();
this.place = noteCreateReq.getPlace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import jakarta.persistence.FetchType;
import jakarta.persistence.OneToMany;
import lombok.Getter;
import team_alcoholic.jumo_server.v1.liquor.domain.Liquor;
import org.hibernate.annotations.BatchSize;
import team_alcoholic.jumo_server.v2.liquor.domain.NewLiquor;
import team_alcoholic.jumo_server.v2.note.dto.request.TastingNoteCreateReq;
import team_alcoholic.jumo_server.v2.note.dto.request.TastingNoteUpdateReq;
import team_alcoholic.jumo_server.v2.user.domain.NewUser;
Expand All @@ -30,10 +31,11 @@ public class TastingNote extends Note {
private String finish;

@OneToMany(mappedBy = "note", fetch = FetchType.LAZY)
@BatchSize(size = 100)
private List<NoteAroma> noteAromas = new ArrayList<>();

protected TastingNote() {}
public TastingNote(TastingNoteCreateReq noteCreateReq, NewUser user, Liquor liquor) {
public TastingNote(TastingNoteCreateReq noteCreateReq, NewUser user, NewLiquor liquor) {
super(user, liquor);
this.tastingAt = noteCreateReq.getTastingAt();
this.method = noteCreateReq.getMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.Getter;
import lombok.Setter;
import team_alcoholic.jumo_server.v1.liquor.dto.LiquorRes;
import team_alcoholic.jumo_server.v2.liquor.dto.LiquorSimpleRes;
import team_alcoholic.jumo_server.v2.user.dto.UserRes;

import java.time.LocalDateTime;
Expand All @@ -17,6 +17,6 @@ public abstract class NoteRes {
private LocalDateTime updatedAt;

private UserRes user;
private LiquorRes liquor;
private LiquorSimpleRes liquor;
private List<NoteImageRes> noteImages = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package team_alcoholic.jumo_server.v2.note.dto.response;

import jakarta.persistence.DiscriminatorValue;
import lombok.Getter;
import lombok.Setter;
import team_alcoholic.jumo_server.v1.liquor.dto.LiquorRes;
import team_alcoholic.jumo_server.v2.liquor.dto.LiquorSimpleRes;
import team_alcoholic.jumo_server.v2.note.domain.NoteImage;
import team_alcoholic.jumo_server.v2.note.domain.PurchaseNote;
import team_alcoholic.jumo_server.v2.user.dto.UserRes;
Expand All @@ -26,7 +25,7 @@ public static PurchaseNoteRes from(PurchaseNote note) {
purchaseNoteRes.setCreatedAt(note.getCreatedAt());
purchaseNoteRes.setUpdatedAt(note.getUpdatedAt());
purchaseNoteRes.setUser(UserRes.from(note.getUser()));
purchaseNoteRes.setLiquor(LiquorRes.from(note.getLiquor()));
purchaseNoteRes.setLiquor(LiquorSimpleRes.from(note.getLiquor()));
for (NoteImage noteImage : note.getNoteImages()) {
purchaseNoteRes.getNoteImages().add(NoteImageRes.from(noteImage));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package team_alcoholic.jumo_server.v2.note.dto.response;

import jakarta.persistence.DiscriminatorValue;
import lombok.Getter;
import lombok.Setter;
import team_alcoholic.jumo_server.v1.liquor.dto.LiquorRes;
import team_alcoholic.jumo_server.v2.aroma.dto.AromaRes;
import team_alcoholic.jumo_server.v2.liquor.dto.LiquorSimpleRes;
import team_alcoholic.jumo_server.v2.note.domain.NoteAroma;
import team_alcoholic.jumo_server.v2.note.domain.NoteImage;
import team_alcoholic.jumo_server.v2.note.domain.TastingNote;
Expand Down Expand Up @@ -35,7 +34,7 @@ public static TastingNoteRes from(TastingNote note) {
tastingNoteRes.setCreatedAt(note.getCreatedAt());
tastingNoteRes.setUpdatedAt(note.getUpdatedAt());
tastingNoteRes.setUser(UserRes.from(note.getUser()));
tastingNoteRes.setLiquor(LiquorRes.from(note.getLiquor()));
tastingNoteRes.setLiquor(LiquorSimpleRes.from(note.getLiquor()));
for (NoteImage noteImage : note.getNoteImages()) {
tastingNoteRes.getNoteImages().add(NoteImageRes.from(noteImage));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import team_alcoholic.jumo_server.v1.liquor.domain.Liquor;
import team_alcoholic.jumo_server.v2.liquor.domain.NewLiquor;
import team_alcoholic.jumo_server.v2.note.domain.Note;
import team_alcoholic.jumo_server.v2.user.domain.NewUser;

Expand Down Expand Up @@ -52,7 +53,7 @@ public interface NoteRepository extends JpaRepository<Note, Long> {
*/
@EntityGraph(attributePaths = {"user", "liquor", "noteImages"})
@Query("select n from Note n left join fetch n.noteImages ni where n.liquor = :liquor order by n.id desc, ni.id")
List<Note> findListByLiquor(Liquor liquor);
List<Note> findListByLiquor(NewLiquor liquor);

/**
* 사용자별 주류별 노트 조회
Expand All @@ -61,5 +62,5 @@ public interface NoteRepository extends JpaRepository<Note, Long> {
*/
@EntityGraph(attributePaths = {"user", "liquor", "noteImages"})
@Query("select n from Note n left join fetch n.noteImages ni where n.user = :user and n.liquor = :liquor order by n.id desc, ni.id")
List<Note> findListByUserAndLiquor(NewUser user, Liquor liquor);
List<Note> findListByUserAndLiquor(NewUser user, NewLiquor liquor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public interface TastingNoteRepository extends JpaRepository<TastingNote, Long>
* @param cursor 마지막으로 조회한 노트의 id
* @param pageable paging
*/
@EntityGraph(attributePaths = {"user", "liquor", "noteImages"})
@EntityGraph(attributePaths = {"user", "liquor", "noteAromas.aroma"})
@Query("select tn from tasting_note_new tn where tn.id < :cursor order by tn.id desc")
List<Note> findListByCursor(Long cursor, Pageable pageable);

/**
* 최신순 노트 페이지네이션 조회
* @param pageable paging
*/
@EntityGraph(attributePaths = {"user", "liquor", "noteImages"})
@EntityGraph(attributePaths = {"user", "liquor", "noteAromas.aroma"})
@Query("select tn from tasting_note_new tn order by tn.id desc")
List<Note> findList(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import team_alcoholic.jumo_server.global.error.exception.UnauthorizedException;
import team_alcoholic.jumo_server.v1.liquor.domain.Liquor;
import team_alcoholic.jumo_server.v1.liquor.exception.LiquorNotFoundException;
import team_alcoholic.jumo_server.v1.liquor.repository.LiquorRepository;
import team_alcoholic.jumo_server.v2.liquor.domain.NewLiquor;
import team_alcoholic.jumo_server.v2.liquor.repository.NewLiquorRepository;
import team_alcoholic.jumo_server.v2.aroma.domain.Aroma;
import team_alcoholic.jumo_server.v2.aroma.repository.AromaRepository;
import team_alcoholic.jumo_server.v2.note.domain.*;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class NoteService {
private final PurchaseNoteRepository purchaseNoteRepository;
private final TastingNoteRepository tastingNoteRepository;
private final UserRepository userRepository;
private final LiquorRepository liquorRepository;
private final NewLiquorRepository liquorRepository;
private final AromaRepository aromaRepository;
private final NoteImageRepository noteImageRepository;
private final NoteAromaRepository noteAromaRepository;
Expand All @@ -57,7 +58,7 @@ public class NoteService {
public PurchaseNoteRes createPurchaseNote(UUID userUuid, PurchaseNoteCreateReq noteCreateReq) throws IOException {
// PurchaseNote 엔티티 생성 및 저장
NewUser user = userRepository.findByUserUuid(userUuid);
Liquor liquor = liquorRepository.findById(noteCreateReq.getLiquorId())
NewLiquor liquor = liquorRepository.findById(noteCreateReq.getLiquorId())
.orElseThrow(() -> new LiquorNotFoundException(noteCreateReq.getLiquorId()));
PurchaseNote purchaseNote = new PurchaseNote(noteCreateReq, user, liquor);
purchaseNoteRepository.save(purchaseNote);
Expand All @@ -84,7 +85,7 @@ public PurchaseNoteRes createPurchaseNote(UUID userUuid, PurchaseNoteCreateReq n
public TastingNoteRes createTastingNote(UUID userUuid, TastingNoteCreateReq noteCreateReq) throws IOException {
// TastingNote 엔티티 생성 및 저장
NewUser user = userRepository.findByUserUuid(userUuid);
Liquor liquor = liquorRepository.findById(noteCreateReq.getLiquorId())
NewLiquor liquor = liquorRepository.findById(noteCreateReq.getLiquorId())
.orElseThrow(() -> new LiquorNotFoundException(noteCreateReq.getLiquorId()));
TastingNote tastingNote = new TastingNote(noteCreateReq, user, liquor);
tastingNoteRepository.save(tastingNote);
Expand Down Expand Up @@ -233,7 +234,7 @@ public List<GeneralNoteRes> getNotesByUserAndLiquor(UUID userUuid, Long liquorId
if (user == null) { throw new UserNotFoundException(userUuid); }

// liquor 조회
Liquor liquor = liquorRepository.findById(liquorId).orElseThrow(() -> new LiquorNotFoundException(liquorId));
NewLiquor liquor = liquorRepository.findById(liquorId).orElseThrow(() -> new LiquorNotFoundException(liquorId));

// note 조회
List<Note> notes = noteRepository.findListByUserAndLiquor(user, liquor);
Expand All @@ -253,7 +254,7 @@ public List<GeneralNoteRes> getNotesByUserAndLiquor(UUID userUuid, Long liquorId
*/
public List<GeneralNoteRes> getNotesByLiquor(Long liquorId) {
// liquor 조회
Liquor liquor = liquorRepository.findById(liquorId)
NewLiquor liquor = liquorRepository.findById(liquorId)
.orElseThrow(() -> new LiquorNotFoundException(liquorId));

// note 조회
Expand Down

0 comments on commit a0616f0

Please sign in to comment.