From a0616f08a0a4e7f3e1206055b0807a6e414d84e3 Mon Sep 17 00:00:00 2001 From: Youngmin Song <68019733+y-ngm-n@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:02:19 +0900 Subject: [PATCH] =?UTF-8?q?[BYOB-232]=20=EA=B5=AC=EB=A7=A4=20=EB=85=B8?= =?UTF-8?q?=ED=8A=B8=20=EB=B0=8F=20=EA=B0=90=EC=83=81=20=EB=85=B8=ED=8A=B8?= =?UTF-8?q?=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20?= =?UTF-8?q?=EB=B0=9C=EC=83=9D=ED=95=98=EB=8A=94=20N+1=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=20=ED=95=B4=EA=B2=B0=20(#75)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Note에 연관된 Liquor를 v2로 변경 * fix: MultipleBagFetchException을 피해서 N+1 문제를 해결하기 위해 BatchSize 적용 --- .../v2/liquor/dto/LiquorSimpleRes.java | 31 +++++++++++++++++++ .../jumo_server/v2/note/domain/Note.java | 8 +++-- .../v2/note/domain/PurchaseNote.java | 3 +- .../v2/note/domain/TastingNote.java | 6 ++-- .../v2/note/dto/response/NoteRes.java | 4 +-- .../v2/note/dto/response/PurchaseNoteRes.java | 5 ++- .../v2/note/dto/response/TastingNoteRes.java | 5 ++- .../v2/note/repository/NoteRepository.java | 5 +-- .../repository/TastingNoteRepository.java | 4 +-- .../v2/note/service/NoteService.java | 13 ++++---- 10 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 src/main/java/team_alcoholic/jumo_server/v2/liquor/dto/LiquorSimpleRes.java diff --git a/src/main/java/team_alcoholic/jumo_server/v2/liquor/dto/LiquorSimpleRes.java b/src/main/java/team_alcoholic/jumo_server/v2/liquor/dto/LiquorSimpleRes.java new file mode 100644 index 0000000..a8f8d0f --- /dev/null +++ b/src/main/java/team_alcoholic/jumo_server/v2/liquor/dto/LiquorSimpleRes.java @@ -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; + } +} diff --git a/src/main/java/team_alcoholic/jumo_server/v2/note/domain/Note.java b/src/main/java/team_alcoholic/jumo_server/v2/note/domain/Note.java index 6db9761..25253b9 100644 --- a/src/main/java/team_alcoholic/jumo_server/v2/note/domain/Note.java +++ b/src/main/java/team_alcoholic/jumo_server/v2/note/domain/Note.java @@ -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; @@ -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 noteImages = new ArrayList<>(); protected Note() {} - public Note(NewUser user, Liquor liquor) { + public Note(NewUser user, NewLiquor liquor) { this.user = user; this.liquor = liquor; } diff --git a/src/main/java/team_alcoholic/jumo_server/v2/note/domain/PurchaseNote.java b/src/main/java/team_alcoholic/jumo_server/v2/note/domain/PurchaseNote.java index dc7c253..f89de99 100644 --- a/src/main/java/team_alcoholic/jumo_server/v2/note/domain/PurchaseNote.java +++ b/src/main/java/team_alcoholic/jumo_server/v2/note/domain/PurchaseNote.java @@ -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; @@ -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(); diff --git a/src/main/java/team_alcoholic/jumo_server/v2/note/domain/TastingNote.java b/src/main/java/team_alcoholic/jumo_server/v2/note/domain/TastingNote.java index 753235a..4bdd36d 100644 --- a/src/main/java/team_alcoholic/jumo_server/v2/note/domain/TastingNote.java +++ b/src/main/java/team_alcoholic/jumo_server/v2/note/domain/TastingNote.java @@ -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; @@ -30,10 +31,11 @@ public class TastingNote extends Note { private String finish; @OneToMany(mappedBy = "note", fetch = FetchType.LAZY) + @BatchSize(size = 100) private List 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(); diff --git a/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/NoteRes.java b/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/NoteRes.java index 7831b6a..501de6b 100644 --- a/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/NoteRes.java +++ b/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/NoteRes.java @@ -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; @@ -17,6 +17,6 @@ public abstract class NoteRes { private LocalDateTime updatedAt; private UserRes user; - private LiquorRes liquor; + private LiquorSimpleRes liquor; private List noteImages = new ArrayList<>(); } diff --git a/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/PurchaseNoteRes.java b/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/PurchaseNoteRes.java index 3dea643..9e3870e 100644 --- a/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/PurchaseNoteRes.java +++ b/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/PurchaseNoteRes.java @@ -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; @@ -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)); } diff --git a/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/TastingNoteRes.java b/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/TastingNoteRes.java index 20779db..8dbd23d 100644 --- a/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/TastingNoteRes.java +++ b/src/main/java/team_alcoholic/jumo_server/v2/note/dto/response/TastingNoteRes.java @@ -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; @@ -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)); } diff --git a/src/main/java/team_alcoholic/jumo_server/v2/note/repository/NoteRepository.java b/src/main/java/team_alcoholic/jumo_server/v2/note/repository/NoteRepository.java index c223b96..001ac1b 100644 --- a/src/main/java/team_alcoholic/jumo_server/v2/note/repository/NoteRepository.java +++ b/src/main/java/team_alcoholic/jumo_server/v2/note/repository/NoteRepository.java @@ -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; @@ -52,7 +53,7 @@ public interface NoteRepository extends JpaRepository { */ @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 findListByLiquor(Liquor liquor); + List findListByLiquor(NewLiquor liquor); /** * 사용자별 주류별 노트 조회 @@ -61,5 +62,5 @@ public interface NoteRepository extends JpaRepository { */ @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 findListByUserAndLiquor(NewUser user, Liquor liquor); + List findListByUserAndLiquor(NewUser user, NewLiquor liquor); } diff --git a/src/main/java/team_alcoholic/jumo_server/v2/note/repository/TastingNoteRepository.java b/src/main/java/team_alcoholic/jumo_server/v2/note/repository/TastingNoteRepository.java index c6d587c..ba118ba 100644 --- a/src/main/java/team_alcoholic/jumo_server/v2/note/repository/TastingNoteRepository.java +++ b/src/main/java/team_alcoholic/jumo_server/v2/note/repository/TastingNoteRepository.java @@ -16,7 +16,7 @@ public interface TastingNoteRepository extends JpaRepository * @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 findListByCursor(Long cursor, Pageable pageable); @@ -24,7 +24,7 @@ public interface TastingNoteRepository extends JpaRepository * 최신순 노트 페이지네이션 조회 * @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 findList(Pageable pageable); } diff --git a/src/main/java/team_alcoholic/jumo_server/v2/note/service/NoteService.java b/src/main/java/team_alcoholic/jumo_server/v2/note/service/NoteService.java index 3919836..a3e9a48 100644 --- a/src/main/java/team_alcoholic/jumo_server/v2/note/service/NoteService.java +++ b/src/main/java/team_alcoholic/jumo_server/v2/note/service/NoteService.java @@ -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.*; @@ -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; @@ -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); @@ -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); @@ -233,7 +234,7 @@ public List 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 notes = noteRepository.findListByUserAndLiquor(user, liquor); @@ -253,7 +254,7 @@ public List getNotesByUserAndLiquor(UUID userUuid, Long liquorId */ public List getNotesByLiquor(Long liquorId) { // liquor 조회 - Liquor liquor = liquorRepository.findById(liquorId) + NewLiquor liquor = liquorRepository.findById(liquorId) .orElseThrow(() -> new LiquorNotFoundException(liquorId)); // note 조회