Skip to content

Commit

Permalink
Merge pull request #394 from woowacourse-teams/refactor/#387
Browse files Browse the repository at this point in the history
[refactor] 감상 조회시 N+1 문제 해결
  • Loading branch information
Jaeyoung22 authored Sep 21, 2023
2 parents 8131095 + fbfa56d commit c82f5fd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post, Long> {

List<Post> findAllByTripId(Long tripId);
@Query("SELECT p FROM Post p JOIN FETCH p.point where p.tripId = :tripId")
List<Post> findAllByTripId(@Param("tripId") Long tripId);

default Post getByPostId(Long id) {
return findById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class PostCustomRepositoryImpl implements PostCustomRepository {
public List<Post> findAllByConditions(PostSearchConditions conditions, PostSearchPaging paging) {
// TODO: 2023/09/16 연령대, 성별 추가
return jpaQueryFactory.selectFrom(post)
.leftJoin(post.point).fetchJoin()
.where(
postIdLt(paging.lastViewedId()),
yearIn(conditions.years()),
Expand Down
6 changes: 4 additions & 2 deletions backend/src/main/java/dev/tripdraw/trip/domain/Point.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit c82f5fd

Please sign in to comment.