Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] 감상 조회시 N+1 문제 해결 #394

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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