Fix : N + 1 문제 해결

This commit is contained in:
김용수 2024-09-12 18:00:10 +09:00
parent 6c46b64ce2
commit 6c2c0670ab
5 changed files with 21 additions and 11 deletions

View File

@ -45,7 +45,7 @@ public class FolderResponse {
public static FolderResponse fromWithNeedReview(final Folder folder) { public static FolderResponse fromWithNeedReview(final Folder folder) {
List<ImageResponse> images = folder.getImageList().stream() List<ImageResponse> images = folder.getImageList().stream()
.filter(image -> image.getStatus() == LabelStatus.NEED_REVIEW) .filter(image -> image.getStatus() == LabelStatus.REVIEW_REQUEST)
.map(ImageResponse::from) .map(ImageResponse::from)
.toList(); .toList();

View File

@ -2,6 +2,8 @@ package com.worlabel.domain.folder.repository;
import com.worlabel.domain.folder.entity.Folder; import com.worlabel.domain.folder.entity.Folder;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
@ -10,9 +12,20 @@ import java.util.Optional;
@Repository @Repository
public interface FolderRepository extends JpaRepository<Folder, Integer> { public interface FolderRepository extends JpaRepository<Folder, Integer> {
List<Folder> findAllByProjectIdAndParentIsNull(Integer projectId); @Query("SELECT f FROM Folder f " +
"LEFT JOIN FETCH f.imageList i " +
"LEFT JOIN FETCH i.label " +
"WHERE f.project.id = :projectId " +
"AND f.parent IS NULL ")
List<Folder> findAllByProjectIdAndParentIsNull(@Param("projectId") Integer projectId);
@Query("SELECT f FROM Folder f " +
"LEFT JOIN FETCH f.imageList i " +
"LEFT JOIN FETCH i.label " +
"WHERE f.project.id = :projectId " +
"AND f.id = :folderId")
Optional<Folder> findAllByProjectIdAndId(@Param("projectId") Integer projectId, @Param("folderId") Integer folderId);
Optional<Folder> findAllByProjectIdAndId(Integer projectId, Integer folderId);
boolean existsByIdAndProjectId(Integer folderId, Integer projectId); boolean existsByIdAndProjectId(Integer folderId, Integer projectId);
} }

View File

@ -46,7 +46,7 @@ public class Image extends BaseEntity {
*/ */
@Column(name = "status", nullable = false) @Column(name = "status", nullable = false)
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
private LabelStatus status = LabelStatus.Pending; private LabelStatus status = LabelStatus.PENDING;
/** /**
* 속한 폴더 * 속한 폴더
@ -59,7 +59,7 @@ public class Image extends BaseEntity {
/** /**
* 이미지에 연결된 레이블 * 이미지에 연결된 레이블
*/ */
@OneToOne(mappedBy = "image", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) @OneToOne(fetch = FetchType.LAZY, mappedBy = "image", cascade = CascadeType.ALL, orphanRemoval = true)
private Label label; private Label label;
private Image(final String imageTitle, final String imageUrl, final Integer order, final Folder folder) { private Image(final String imageTitle, final String imageUrl, final Integer order, final Folder folder) {

View File

@ -4,9 +4,10 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.annotation.JsonValue;
public enum LabelStatus { public enum LabelStatus {
Pending, PENDING,
IN_PROGRESS, IN_PROGRESS,
NEED_REVIEW, SAVE,
REVIEW_REQUEST,
COMPLETED; COMPLETED;
// 입력 값을 enum 값과 일치시키기 위해 대소문자 구분 없이 변환 // 입력 값을 enum 값과 일치시키기 위해 대소문자 구분 없이 변환

View File

@ -40,8 +40,4 @@ public class Label extends BaseEntity {
label.image = image; label.image = image;
return label; return label;
} }
public void changeUrl(String newUrl){
}
} }