diff --git a/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java b/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java index f8c12c541..7baa16bd7 100644 --- a/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java +++ b/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java @@ -4,12 +4,15 @@ import dev.tripdraw.post.exception.PostException; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; public interface PostRepository extends JpaRepository { - List findAllByTripId(Long tripId); + @Query("SELECT p FROM Post p JOIN FETCH p.point where p.tripId = :tripId") + List findAllByTripId(@Param("tripId") Long tripId); default Post getByPostId(Long id) { return findById(id) diff --git a/backend/src/main/java/dev/tripdraw/post/query/PostCustomRepositoryImpl.java b/backend/src/main/java/dev/tripdraw/post/query/PostCustomRepositoryImpl.java index 9d713bc80..34fa8c9dd 100644 --- a/backend/src/main/java/dev/tripdraw/post/query/PostCustomRepositoryImpl.java +++ b/backend/src/main/java/dev/tripdraw/post/query/PostCustomRepositoryImpl.java @@ -23,6 +23,7 @@ public class PostCustomRepositoryImpl implements PostCustomRepository { public List findAllByConditions(PostSearchConditions conditions, PostSearchPaging paging) { // TODO: 2023/09/16 연령대, 성별 추가 return jpaQueryFactory.selectFrom(post) + .leftJoin(post.point).fetchJoin() .where( postIdLt(paging.lastViewedId()), yearIn(conditions.years()), diff --git a/backend/src/main/java/dev/tripdraw/trip/domain/Point.java b/backend/src/main/java/dev/tripdraw/trip/domain/Point.java index 84e916d21..45a28e33e 100644 --- a/backend/src/main/java/dev/tripdraw/trip/domain/Point.java +++ b/backend/src/main/java/dev/tripdraw/trip/domain/Point.java @@ -1,6 +1,7 @@ package dev.tripdraw.trip.domain; import static dev.tripdraw.trip.exception.TripExceptionType.POINT_ALREADY_HAS_POST; +import static jakarta.persistence.FetchType.LAZY; import static jakarta.persistence.GenerationType.IDENTITY; import static lombok.AccessLevel.PROTECTED; @@ -12,11 +13,12 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; -import java.time.LocalDateTime; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; +import java.time.LocalDateTime; + @Accessors(fluent = true) @Getter @NoArgsConstructor(access = PROTECTED) @@ -41,7 +43,7 @@ public class Point extends BaseEntity { private LocalDateTime recordedAt; @JoinColumn(name = "trip_id") - @ManyToOne + @ManyToOne(fetch = LAZY) private Trip trip; public Point(Double latitude, Double longitude, LocalDateTime recordedAt) {