[Project] in query 테스트 코드 작성

김호정's avatar
Oct 09, 2024
[Project] in query 테스트 코드 작성
 
 
영화관 & 상영관 & 상영 정보를 모두 조회해 오는 걸 inquery 를 사용해서 작성해 보았다.
 
 
notion image
package shop.mtcoding.filmtalk.cinema; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.test.context.TestPropertySource; import shop.mtcoding.filmtalk.screen.Screen; import shop.mtcoding.filmtalk.screen.ScreenRepository; import shop.mtcoding.filmtalk.showtime.Showtime; import shop.mtcoding.filmtalk.showtime.ShowtimeRepository; import shop.mtcoding.filmtalk.user.UserRepository; import java.util.List; @DataJpaTest public class CinemaRepositoryTest { @Autowired private CinemaRepository cinemaRepository; @Autowired private ScreenRepository screenRepository; @Autowired private ShowtimeRepository showtimeRepository; @Test public void mFindCinemaById_test() { List<Cinema> cinemas = cinemaRepository.findAll(); System.out.println("=========================="); List<Long> cinemaIds = cinemas.stream().map(cinema -> cinema.getId()).toList(); List<Screen> screens = screenRepository.findByCinemaIds(cinemaIds); System.out.println("=========================="); List<Long> screenIds = screens.stream().map(screen -> screen.getId()).toList(); List<Showtime> showtimes = showtimeRepository.findByScreenIds(screenIds); System.out.println("=========================="); for (Screen screen : screens) { System.out.println(" Screen ID: " + screen.getId()); System.out.println(" Screen Name: " + screen.getName()); System.out.println(" ------------"); for (Showtime showtime : showtimes) { System.out.println(" Showtime ID: " + showtime.getId()); System.out.println(" 상영 시작 시간: " + showtime.getStartedAt()); } } } }
notion image
 
 
package shop.mtcoding.filmtalk.cinema; 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 CinemaRepository extends JpaRepository<Cinema, Long> { // @Query("SELECT c FROM Cinema c " + // "JOIN FETCH c.screens scr " + // "JOIN FETCH scr.showtimes " + // "WHERE c.id = :cinemaId") // Cinema findCinemaWithScreensAndShowtimes(@Param("cinemaId") Long cinemaId); @Query("select c from Cinema c join fetch c.screens s where c.id=:id") Cinema mFindCinemaById(@Param("id") Long id); @Query("select c from Cinema c join fetch c.screens s where c.id in :cinemaIds") List<Cinema> mFindIdsByIds(@Param("cinemaIds") List<Long> cinemaIds); }
 
 
 
notion image
package shop.mtcoding.filmtalk.screen; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import shop.mtcoding.filmtalk.showtime.Showtime; import java.util.List; public interface ScreenRepository extends JpaRepository<Screen, Long> { @Query("select s from Screen s where s.cinema.id in :cinemaIds") List<Screen> findByCinemaIds(@Param("cinemaIds") List<Long> cinemaIds); }
 
 
 
notion image
package shop.mtcoding.filmtalk.showtime; 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; import java.util.Optional; public interface ShowtimeRepository extends JpaRepository<Showtime,Long> { @Query("select st from Showtime st join fetch st.movie m left join fetch st.screen sc where st.id=:id") Showtime mFindById(@Param("id") Long id); @Query("select s from Showtime s where s.screen.id in :screenIds") List<Showtime> findByScreenIds(@Param("screenIds") List<Long> screenIds); }
 
 
notion image
notion image
 
→ 3번 SELECT 해서 모든 정보를 가져 올 수 있다.
 
Share article

keepgoing