Skip to content

Commit 000e609

Browse files
committed
refactor: EmotionRecord JPQL -> QueryDSL 리팩토링 #21
1 parent b5588f2 commit 000e609

File tree

3 files changed

+94
-11
lines changed

3 files changed

+94
-11
lines changed

src/main/java/org/dfbf/soundlink/domain/emotionRecord/repository/EmotionRecordRepository.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import org.dfbf.soundlink.domain.emotionRecord.entity.EmotionRecord;
44
import org.dfbf.soundlink.domain.emotionRecord.repository.dsl.EmotionRecordRepositoryCustom;
5-
import org.dfbf.soundlink.domain.user.dto.response.EmotionRecordDto;
65
import org.dfbf.soundlink.domain.user.entity.User;
76
import org.springframework.data.domain.Page;
87
import org.springframework.data.domain.Pageable;
98
import org.springframework.data.jpa.repository.*;
109
import org.springframework.data.repository.query.Param;
1110
import org.springframework.stereotype.Repository;
1211

13-
import java.util.List;
1412
import java.util.Optional;
1513

1614
@Repository
@@ -20,21 +18,12 @@ public interface EmotionRecordRepository extends JpaRepository<EmotionRecord, Lo
2018
@Query("DELETE FROM EmotionRecord e WHERE e.user = :user")
2119
public void deleteByUser(@Param("user") User user);
2220

23-
@Query("SELECT er FROM EmotionRecord er " +
24-
"JOIN FETCH er.user u " +
25-
"JOIN FETCH er.spotifyMusic sm " +
26-
"WHERE u.loginId = :loginId")
2721
Page<EmotionRecord> findByLoginId(@Param("loginId") String loginId, Pageable pageable);
2822

29-
@Query("SELECT er FROM EmotionRecord er " +
30-
"JOIN FETCH er.user u " +
31-
"LEFT JOIN FETCH er.spotifyMusic sm " +
32-
"WHERE u.userId <> :userId ")
3323
Page<EmotionRecord> findByWithoutUserId(@Param("userId") Long userId, Pageable pageable);
3424

3525
Optional<EmotionRecord> findByRecordId(@Param("recordId") Long recordId);
3626

37-
@Modifying
3827
int deleteByRecordId(@Param("recordId") Long recordId);
3928
}
4029

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
package org.dfbf.soundlink.domain.emotionRecord.repository.dsl;
22

3+
import org.dfbf.soundlink.domain.emotionRecord.entity.EmotionRecord;
34
import org.dfbf.soundlink.domain.user.dto.response.EmotionRecordDto;
45
import org.dfbf.soundlink.domain.user.entity.User;
6+
import org.springframework.data.domain.Page;
7+
import org.springframework.data.domain.Pageable;
58

69
import java.util.List;
10+
import java.util.Optional;
711

812
public interface EmotionRecordRepositoryCustom {
913

1014
List<EmotionRecordDto> findByUser(User user);
15+
16+
Page<EmotionRecord> findByLoginId(String loginId, Pageable pageable);
17+
18+
Page<EmotionRecord> findByWithoutUserId(Long userId, Pageable pageable);
19+
20+
Optional<EmotionRecord> findByRecordId(Long recordId);
21+
22+
int deleteByRecordId(Long recordId);
1123
}

src/main/java/org/dfbf/soundlink/domain/emotionRecord/repository/dsl/EmotionRecordRepositoryImpl.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@
33
import com.querydsl.core.types.Projections;
44
import com.querydsl.jpa.impl.JPAQueryFactory;
55
import lombok.RequiredArgsConstructor;
6+
import org.dfbf.soundlink.domain.emotionRecord.entity.EmotionRecord;
67
import org.dfbf.soundlink.domain.emotionRecord.entity.QEmotionRecord;
8+
import org.dfbf.soundlink.domain.emotionRecord.entity.QSpotifyMusic;
79
import org.dfbf.soundlink.domain.user.dto.response.EmotionRecordDto;
10+
import org.dfbf.soundlink.domain.user.entity.QUser;
811
import org.dfbf.soundlink.domain.user.entity.User;
12+
import org.springframework.data.domain.Page;
13+
import org.springframework.data.domain.PageImpl;
14+
import org.springframework.data.domain.Pageable;
915
import org.springframework.stereotype.Repository;
1016

1117
import java.util.List;
18+
import java.util.Optional;
1219

1320
@Repository
1421
@RequiredArgsConstructor
1522
public class EmotionRecordRepositoryImpl implements EmotionRecordRepositoryCustom {
1623

1724
private final JPAQueryFactory jpaQueryFactory;
25+
private final QEmotionRecord emotionRecord = QEmotionRecord.emotionRecord;
26+
private final QUser user = QUser.user;
27+
private final QSpotifyMusic spotifyMusic = QSpotifyMusic.spotifyMusic;
28+
1829

1930
@Override
2031
public List<EmotionRecordDto> findByUser(User user) {
@@ -34,4 +45,75 @@ public List<EmotionRecordDto> findByUser(User user) {
3445
.fetch();
3546

3647
}
48+
49+
// loginId를 기준으로 JOIN FETCH (user, spotifyMusic) 후 페이징 처리
50+
@Override
51+
public Page<EmotionRecord> findByLoginId(String loginId, Pageable pageable) {
52+
List<EmotionRecord> content = jpaQueryFactory
53+
.selectFrom(emotionRecord)
54+
.join(emotionRecord.user, user).fetchJoin()
55+
.join(emotionRecord.spotifyMusic, spotifyMusic).fetchJoin()
56+
.where(user.loginId.eq(loginId))
57+
.offset(pageable.getOffset())
58+
.limit(pageable.getPageSize())
59+
.fetch();
60+
61+
// Spring Data JPA에서 페이징 처리를 위한 메서드 사용 시,
62+
// 내부적으로 데이터(페이징된 결과)를 가져오는 쿼리와 전체 데이터 수를 계산하는 쿼리가 둘 다 실행됨
63+
// QueryDSL을 사용할 경우에 위와 달리 데이터 수 계산 쿼리를 별도로 실행해 줘야함 (Querydsl 5 이상 권장 방식)
64+
long total = Optional.ofNullable(
65+
jpaQueryFactory
66+
.select(emotionRecord.count())
67+
.from(emotionRecord)
68+
.join(emotionRecord.user, user)
69+
.where(user.loginId.eq(loginId))
70+
.fetchOne()
71+
).orElse(0L);
72+
73+
return new PageImpl<>(content, pageable, total);
74+
}
75+
76+
// 로그인 된 userId를 제외한 EmotionRecord 조회 (LEFT JOIN으로 spotifyMusic 포함) 후 페이징처리
77+
@Override
78+
public Page<EmotionRecord> findByWithoutUserId(Long userId, Pageable pageable) {
79+
List<EmotionRecord> content = jpaQueryFactory
80+
.selectFrom(emotionRecord)
81+
.join(emotionRecord.user, user).fetchJoin()
82+
.leftJoin(emotionRecord.spotifyMusic, spotifyMusic).fetchJoin()
83+
.where(user.userId.ne(userId))
84+
.offset(pageable.getOffset())
85+
.limit(pageable.getPageSize())
86+
.fetch();
87+
88+
// 위의 findByLoginId 메서드 설명 참고
89+
long total = Optional.ofNullable(
90+
jpaQueryFactory
91+
.select(emotionRecord.count())
92+
.from(emotionRecord)
93+
.join(emotionRecord.user, user)
94+
.where(user.userId.ne(userId))
95+
.fetchOne()
96+
).orElse(0L);
97+
98+
return new PageImpl<>(content, pageable, total);
99+
}
100+
101+
// recordId에 해당하는 EmotionRecord 조회
102+
@Override
103+
public Optional<EmotionRecord> findByRecordId(Long recordId) {
104+
EmotionRecord record = jpaQueryFactory
105+
.selectFrom(emotionRecord)
106+
.where(emotionRecord.recordId.eq(recordId))
107+
.fetchOne();
108+
return Optional.ofNullable(record);
109+
}
110+
111+
// recordId에 해당하는 EmotionRecord 삭제
112+
@Override
113+
public int deleteByRecordId(Long recordId) {
114+
return (int)jpaQueryFactory
115+
.delete(emotionRecord)
116+
.where(emotionRecord.recordId.eq(recordId))
117+
.execute();
118+
}
37119
}

0 commit comments

Comments
 (0)