JPQL도 SQL처럼 서브 쿼리를 지원한다.
사용 가능 : where
, having
절
사용 불가능 : select
, from
절
@Test
public void 서브쿼리를_이용한_메뉴_조회_테스트() {
//given
String categoryNameParameter = "한식";
//when
String jpql ="SELECT m FROM section07_menu m WHERE m.categoryCode"
+ "=(SELECT c.categoryCode FROM section07_category c WHERE c.categoryName = :categoryName)";
List<Menu> menuList = entityManager.createQuery(jpql, Menu.class)
.setParameter("categoryName", categoryNameParameter)
.getResultList();
//then
assertNotNull(menuList);
menuList.forEach(System.out::println);
}