Skip to content

Commit

Permalink
fix: 쿼리 결과 두 개 이상의 반환 값이 존재할 때 발생 하는 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
ss7622 committed Jul 12, 2024
1 parent a50cc8c commit 496becf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface FlowerRepository extends JpaRepository<Flower, Long> {
@Query("SELECT f FROM Flower f WHERE f.period BETWEEN :startPeriod AND :endPeriod")
List<Flower> findByPeriodMonth(@Param("startPeriod") Long startPeriod, @Param("endPeriod") Long endPeriod);

Optional<Flower> findByNameAndFlowerLanguage(String name,String flowerLanguage);
List<Flower> findByNameAndFlowerLanguage(String name,String flowerLanguage);

List<Flower> findByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,13 @@ public Flower findByNameAndFlowerLanguage(String name,String flowerLangauge){
flowerLangauge = flowerLangauge.trim();

log.info("Searching for flower with name: '{}' and flowerLanguage: '{}'", name, flowerLangauge);
Flower flower = flowerRepository.findByNameAndFlowerLanguage(name,flowerLangauge).orElseThrow(
() -> new CustomException("꽃 이름 또는 꽃말이 존재하지 않습니다."));
List<Flower> byNameAndFlowerLanguage = flowerRepository.findByNameAndFlowerLanguage(name, flowerLangauge);

return flower;
if(byNameAndFlowerLanguage.isEmpty()){
throw new CustomException("일치하는 꽃 이름과 꽃말이 존재하지 않습니다.");
}

return byNameAndFlowerLanguage.get(0);
}

public Flower findByName(String name){
Expand Down

0 comments on commit 496becf

Please sign in to comment.