Skip to content

Commit

Permalink
[Fix] - JPQL 관련 alias ambiguous 예외 발생 (woowacourse-teams#352)
Browse files Browse the repository at this point in the history
* fix: 중복되는 table alias 변경

* fix: 예외 발생 중인 JPQL을 대체하는 QueryDSL 구현

* fix: 여행기 삭제 및 수정 기능 쿼리 수정
  • Loading branch information
hangillee committed Aug 20, 2024
1 parent 732dd19 commit ab0594d
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package kr.touroot.travelogue.repository;

import java.util.List;
import kr.touroot.travelogue.domain.Travelogue;
import kr.touroot.travelogue.domain.TraveloguePhoto;
import kr.touroot.travelogue.domain.TraveloguePlace;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface TraveloguePhotoRepository extends JpaRepository<TraveloguePhoto, Long> {

List<TraveloguePhoto> findByTraveloguePlace(TraveloguePlace traveloguePlace);

@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query("UPDATE TraveloguePhoto to SET to.deletedAt = NOW() WHERE to.traveloguePlace.travelogueDay.travelogue = :travelogue")
void deleteAllByTraveloguePlaceTravelogueDayTravelogue(Travelogue travelogue);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package kr.touroot.travelogue.repository;

import java.util.List;
import kr.touroot.travelogue.domain.Travelogue;
import kr.touroot.travelogue.domain.TravelogueDay;
import kr.touroot.travelogue.domain.TraveloguePlace;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface TraveloguePlaceRepository extends JpaRepository<TraveloguePlace, Long> {

List<TraveloguePlace> findByTravelogueDay(TravelogueDay travelogueDay);

@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query("UPDATE TraveloguePlace tp SET tp.deletedAt = NOW() WHERE tp.travelogueDay.travelogue = :travelogue")
void deleteAllByTravelogueDayTravelogue(Travelogue travelogue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
import kr.touroot.travelogue.domain.Travelogue;
import kr.touroot.travelogue.domain.TravelogueTag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface TravelogueTagRepository extends JpaRepository<TravelogueTag, Long> {

List<TravelogueTag> findAllByTravelogue(Travelogue travelogue);

@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query("DELETE TravelogueTag tt WHERE tt.travelogue = :travelogue")
void deleteAllByTravelogue(Travelogue travelogue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.touroot.travelogue.repository.query;

import kr.touroot.travelogue.domain.Travelogue;

public interface TravelogueDayQueryRepository {

void deleteAllByTravelogue(Travelogue travelogue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.touroot.travelogue.repository.query;

import static kr.touroot.travelogue.domain.QTravelogueDay.travelogueDay;

import com.querydsl.jpa.impl.JPAQueryFactory;
import java.time.LocalDateTime;
import kr.touroot.travelogue.domain.Travelogue;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class TravelogueDayQueryRepositoryImpl implements TravelogueDayQueryRepository {

private final JPAQueryFactory jpaQueryFactory;

@Override
public void deleteAllByTravelogue(Travelogue travelogue) {
jpaQueryFactory.update(travelogueDay)
.set(travelogueDay.deletedAt, LocalDateTime.now())
.where(travelogueDay.travelogue.eq(travelogue))
.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.touroot.travelogue.repository.query;

import kr.touroot.travelogue.domain.Travelogue;

public interface TraveloguePhotoQueryRepository {

void deleteAllByTravelogue(Travelogue travelogue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kr.touroot.travelogue.repository.query;

import static kr.touroot.travelogue.domain.QTravelogueDay.travelogueDay;
import static kr.touroot.travelogue.domain.QTraveloguePhoto.traveloguePhoto;

import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.time.LocalDateTime;
import kr.touroot.travelogue.domain.QTravelogue;
import kr.touroot.travelogue.domain.Travelogue;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class TraveloguePhotoQueryRepositoryImpl implements TraveloguePhotoQueryRepository {

private final JPAQueryFactory jpaQueryFactory;

@Override
public void deleteAllByTravelogue(Travelogue travelogue) {
jpaQueryFactory.update(traveloguePhoto)
.set(traveloguePhoto.deletedAt, LocalDateTime.now())
.where(traveloguePhoto.traveloguePlace.travelogueDay.eq(
JPAExpressions.selectFrom(travelogueDay)
.leftJoin(QTravelogue.travelogue)
.on(QTravelogue.travelogue.eq(travelogue))
))
.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.touroot.travelogue.repository.query;

import kr.touroot.travelogue.domain.Travelogue;

public interface TraveloguePlaceQueryRepository {

void deleteAllByTravelogue(Travelogue travelogue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.touroot.travelogue.repository.query;

import static kr.touroot.travelogue.domain.QTraveloguePlace.traveloguePlace;

import com.querydsl.jpa.impl.JPAQueryFactory;
import java.time.LocalDateTime;
import kr.touroot.travelogue.domain.Travelogue;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class TraveloguePlaceQueryRepositoryImpl implements TraveloguePlaceQueryRepository {

private final JPAQueryFactory jpaQueryFactory;

@Override
public void deleteAllByTravelogue(Travelogue travelogue) {
jpaQueryFactory.update(traveloguePlace)
.set(traveloguePlace.deletedAt, LocalDateTime.now())
.where(traveloguePlace.travelogueDay.travelogue.eq(travelogue))
.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.touroot.travelogue.repository.query;

import kr.touroot.travelogue.domain.Travelogue;

public interface TravelogueTagQueryRepository {

void deleteAllByTravelogue(Travelogue travelogue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kr.touroot.travelogue.repository.query;

import static kr.touroot.travelogue.domain.QTravelogueTag.travelogueTag;

import com.querydsl.jpa.impl.JPAQueryFactory;
import kr.touroot.travelogue.domain.Travelogue;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class TravelogueTagQueryRepositoryImpl implements TravelogueTagQueryRepository {

private final JPAQueryFactory jpaQueryFactory;

@Override
public void deleteAllByTravelogue(Travelogue travelogue) {
jpaQueryFactory.delete(travelogueTag)
.where(travelogueTag.travelogue.eq(travelogue))
.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import kr.touroot.travelogue.dto.request.TravelogueDayRequest;
import kr.touroot.travelogue.dto.request.TraveloguePlaceRequest;
import kr.touroot.travelogue.repository.TravelogueDayRepository;
import kr.touroot.travelogue.repository.query.TravelogueDayQueryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -18,6 +19,7 @@
public class TravelogueDayService {

private final TravelogueDayRepository travelogueDayRepository;
private final TravelogueDayQueryRepository travelogueDayQueryRepository;

@Transactional
public Map<TravelogueDay, List<TraveloguePlaceRequest>> createDays(
Expand Down Expand Up @@ -48,6 +50,6 @@ public TravelogueDay findDayById(Long id) {

@Transactional
public void deleteAllByTravelogue(Travelogue travelogue) {
travelogueDayRepository.deleteAllByTravelogue(travelogue);
travelogueDayQueryRepository.deleteAllByTravelogue(travelogue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import kr.touroot.travelogue.domain.TraveloguePlace;
import kr.touroot.travelogue.dto.request.TraveloguePhotoRequest;
import kr.touroot.travelogue.repository.TraveloguePhotoRepository;
import kr.touroot.travelogue.repository.query.TraveloguePhotoQueryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -19,6 +20,7 @@ public class TraveloguePhotoService {

private final TraveloguePhotoRepository traveloguePhotoRepository;
private final AwsS3Provider s3Provider;
private final TraveloguePhotoQueryRepository traveloguePhotoQueryRepository;

@Transactional
public List<TraveloguePhoto> createPhotos(List<TraveloguePhotoRequest> requests, TraveloguePlace place) {
Expand Down Expand Up @@ -46,6 +48,6 @@ public List<String> findPhotoUrlsByPlace(TraveloguePlace traveloguePlace) {

@Transactional
public void deleteAllByTravelogue(Travelogue travelogue) {
traveloguePhotoRepository.deleteAllByTraveloguePlaceTravelogueDayTravelogue(travelogue);
traveloguePhotoQueryRepository.deleteAllByTravelogue(travelogue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import kr.touroot.travelogue.dto.request.TraveloguePhotoRequest;
import kr.touroot.travelogue.dto.request.TraveloguePlaceRequest;
import kr.touroot.travelogue.repository.TraveloguePlaceRepository;
import kr.touroot.travelogue.repository.query.TraveloguePlaceQueryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -22,6 +23,7 @@ public class TraveloguePlaceService {

private final PlaceRepository placeRepository;
private final TraveloguePlaceRepository traveloguePlaceRepository;
private final TraveloguePlaceQueryRepository traveloguePlaceQueryRepository;

@Transactional
public Map<TraveloguePlace, List<TraveloguePhotoRequest>> createPlaces(
Expand Down Expand Up @@ -62,6 +64,6 @@ public TraveloguePlace findTraveloguePlaceById(Long id) {

@Transactional
public void deleteAllByTravelogue(Travelogue travelogue) {
traveloguePlaceRepository.deleteAllByTravelogueDayTravelogue(travelogue);
traveloguePlaceQueryRepository.deleteAllByTravelogue(travelogue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import kr.touroot.travelogue.domain.Travelogue;
import kr.touroot.travelogue.domain.TravelogueTag;
import kr.touroot.travelogue.repository.TravelogueTagRepository;
import kr.touroot.travelogue.repository.query.TravelogueTagQueryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -18,6 +19,7 @@ public class TravelogueTagService {

private final TagRepository tagRepository;
private final TravelogueTagRepository travelogueTagRepository;
private final TravelogueTagQueryRepository travelogueTagQueryRepository;

@Transactional
public List<TagResponse> createTravelogueTags(Travelogue travelogue, List<Long> tagIds) {
Expand All @@ -43,7 +45,7 @@ public List<TagResponse> readTagByTravelogue(Travelogue travelogue) {

@Transactional(readOnly = true)
public List<TagResponse> updateTravelogueTags(Travelogue travelogue, List<Long> tagIds) {
travelogueTagRepository.deleteAllByTravelogue(travelogue);
travelogueTagQueryRepository.deleteAllByTravelogue(travelogue);

return tagIds.stream()
.map(id -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import kr.touroot.travelogue.repository.query.TravelogueDayQueryRepository;
import kr.touroot.travelogue.repository.query.TravelogueDayQueryRepositoryImpl;
import kr.touroot.travelogue.repository.query.TraveloguePhotoQueryRepository;
import kr.touroot.travelogue.repository.query.TraveloguePhotoQueryRepositoryImpl;
import kr.touroot.travelogue.repository.query.TraveloguePlaceQueryRepository;
import kr.touroot.travelogue.repository.query.TraveloguePlaceQueryRepositoryImpl;
import kr.touroot.travelogue.repository.query.TravelogueQueryRepository;
import kr.touroot.travelogue.repository.query.TravelogueQueryRepositoryImpl;
import kr.touroot.travelogue.repository.query.TravelogueTagQueryRepository;
import kr.touroot.travelogue.repository.query.TravelogueTagQueryRepositoryImpl;
import kr.touroot.travelplan.repository.PlaceTodoQueryRepository;
import kr.touroot.travelplan.repository.PlaceTodoQueryRepositoryImpl;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,6 +36,26 @@ public TravelogueQueryRepository travelogueQueryRepository() {
return new TravelogueQueryRepositoryImpl(jpaQueryFactory());
}

@Bean
public TravelogueDayQueryRepository travelogueDayQueryRepository() {
return new TravelogueDayQueryRepositoryImpl(jpaQueryFactory());
}

@Bean
public TraveloguePlaceQueryRepository traveloguePlaceQueryRepository() {
return new TraveloguePlaceQueryRepositoryImpl(jpaQueryFactory());
}

@Bean
public TraveloguePhotoQueryRepository traveloguePhotoQueryRepository() {
return new TraveloguePhotoQueryRepositoryImpl(jpaQueryFactory());
}

@Bean
public TravelogueTagQueryRepository travelogueTagQueryRepository() {
return new TravelogueTagQueryRepositoryImpl(jpaQueryFactory());
}

@Bean
public PlaceTodoQueryRepository placeTodoQueryRepository() {
return new PlaceTodoQueryRepositoryImpl(jpaQueryFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import kr.touroot.global.ServiceTest;
import kr.touroot.global.config.TestQueryDslConfig;
import kr.touroot.global.exception.BadRequestException;
import kr.touroot.tag.domain.Tag;
import kr.touroot.tag.dto.TagCreateRequest;
Expand All @@ -16,7 +17,7 @@
import org.springframework.context.annotation.Import;

@DisplayName("태그 서비스")
@Import(value = {TagService.class, TagTestHelper.class})
@Import(value = {TagService.class, TagTestHelper.class, TestQueryDslConfig.class})
@ServiceTest
class TagServiceTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Map;
import kr.touroot.global.ServiceTest;
import kr.touroot.global.config.TestQueryDslConfig;
import kr.touroot.global.exception.BadRequestException;
import kr.touroot.member.domain.Member;
import kr.touroot.travelogue.domain.Travelogue;
Expand All @@ -26,7 +27,7 @@
import org.springframework.context.annotation.Import;

@DisplayName("여행기 일자 서비스")
@Import(value = {TravelogueDayService.class, TravelogueTestHelper.class})
@Import(value = {TravelogueDayService.class, TravelogueTestHelper.class, TestQueryDslConfig.class})
@ServiceTest
class TravelogueDayServiceTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.List;
import kr.touroot.global.ServiceTest;
import kr.touroot.global.config.TestQueryDslConfig;
import kr.touroot.image.infrastructure.AwsS3Provider;
import kr.touroot.member.domain.Member;
import kr.touroot.place.domain.Place;
Expand All @@ -26,7 +27,8 @@
import org.springframework.context.annotation.Import;

@DisplayName("여행기 사진 서비스")
@Import(value = {TraveloguePhotoService.class, TravelogueTestHelper.class, AwsS3Provider.class})
@Import(value = {TraveloguePhotoService.class, TravelogueTestHelper.class, AwsS3Provider.class,
TestQueryDslConfig.class})
@ServiceTest
class TraveloguePhotoServiceTest {

Expand Down
Loading

0 comments on commit ab0594d

Please sign in to comment.