Skip to content

Commit 596ea00

Browse files
committed
fix: Specification 만들 때, join() 과 get() 구분 실시
1 parent 2999e72 commit 596ea00

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/main/java/eatda/repository/cheer/CheerRepository.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import eatda.domain.store.District;
77
import eatda.domain.store.Store;
88
import eatda.domain.store.StoreCategory;
9+
import jakarta.persistence.criteria.JoinType;
910
import java.util.List;
1011
import org.springframework.data.domain.PageRequest;
1112
import org.springframework.data.domain.Pageable;
@@ -19,20 +20,26 @@ public interface CheerRepository extends JpaRepository<Cheer, Long> {
1920
@EntityGraph(attributePaths = {"member", "cheerTags.values"})
2021
List<Cheer> findAllByStoreOrderByCreatedAtDesc(Store store, PageRequest pageRequest);
2122

22-
default List<Cheer> findAllByConditions(@Nullable StoreCategory category, List<CheerTagName> cheerTagNames,
23+
default List<Cheer> findAllByConditions(@Nullable StoreCategory category,
24+
List<CheerTagName> cheerTagNames,
2325
List<District> districts, Pageable pageable) {
2426
Specification<Cheer> spec = createSpecification(category, cheerTagNames, districts);
2527
return findAll(spec, pageable);
2628
}
2729

28-
private Specification<Cheer> createSpecification(@Nullable StoreCategory category, List<CheerTagName> cheerTagNames,
30+
private Specification<Cheer> createSpecification(@Nullable StoreCategory category,
31+
List<CheerTagName> cheerTagNames,
2932
List<District> districts) {
3033
Specification<Cheer> spec = Specification.allOf();
3134
if (category != null) {
3235
spec = spec.and((root, query, cb) -> cb.equal(root.get("store").get("category"), category));
3336
}
3437
if (!cheerTagNames.isEmpty()) {
35-
spec = spec.and(((root, query, cb) -> root.get("cheerTags").get("values").get("name").in(cheerTagNames)));
38+
spec = spec.and(((root, query, cb) -> {
39+
query.distinct(true);
40+
return root.join("cheerTags").join("values", JoinType.LEFT)
41+
.get("name").in(cheerTagNames);
42+
}));
3643
}
3744
if (!districts.isEmpty()) {
3845
spec = spec.and((root, query, cb) -> root.get("store").get("district").in(districts));

src/main/java/eatda/repository/store/StoreRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private Specification<Store> createSpecification(@Nullable StoreCategory categor
5353
}
5454
if (!cheerTagNames.isEmpty()) {
5555
spec = spec.and(((root, query, cb) ->
56-
root.join("cheers").get("cheerTags").get("values").get("name").in(cheerTagNames)));
56+
root.join("cheers").join("cheerTags").join("values").get("name").in(cheerTagNames)));
5757
}
5858
if (!districts.isEmpty()) {
5959
spec = spec.and((root, query, cb) -> root.get("district").in(districts));

0 commit comments

Comments
 (0)