error log
2022-04-12 15:27:58.833 ERROR 58221
--- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] :
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting "all", found 'o' near line 1, column 56 [select o from jpabook.jpashop.domain.Order ojoin fetch o.member mjoin fetch o.delivery d]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting "all", found 'o' near line 1, column 56 [select o from jpabook.jpashop.domain.Order ojoin fetch o.member mjoin fetch o.delivery d]] with root cause
org.hibernate.hql.internal.ast.QuerySyntaxException: expecting "all", found 'o' near line 1, column 56 [select o from jpabook.jpashop.domain.Order ojoin fetch o.member mjoin fetch o.delivery d]
postman에서 v3를 조회하는 순간 아래같이 에러
http://localhost:8080/api/v3/simple-orders
// OrderSimpleApiController.java
@GetMapping("/api/v3/simple-orders")
public List<SimpleOrderDto> orderV3() {
List<Order> orders = orderRepository.findAllWithMemberDelivery();
List<SimpleOrderDto> result = orders.stream()
.map(o -> new SimpleOrderDto(o))
.collect(Collectors.toList());
return result;
}
// OrderRepository.java
public List<Order> findAllWithMemberDelivery() {
return em.createQuery(
"select o from Order o" +
"join fetch o.member m" +
"join fetch o.delivery d", Order.class
).getResultList();
}
해결
OrderRepository의 JPQL문을 작성할 때 " join"으로 안해줬기 때문이다.
아래와 같이 해준다.
public List<Order> findAllWithMemberDelivery() {
return em.createQuery(
"select o from Order o" +
" join fetch o.member m" +
" join fetch o.delivery d", Order.class)
.getResultList();
}
<출처 김영한: 실전! 스프링 부트와 JPA 활용2 - API 개발과 성능 최적화 >
'Spring > JPA' 카테고리의 다른 글
데이터베이스 스키마 자동 생성 (0) | 2022.04.14 |
---|---|
엔티티 매핑 - 객체와 테이블 매핑 (0) | 2022.04.14 |
플러시 & 준영속 상태 정리 (0) | 2022.04.12 |
내부 동작 방식 - 영속성 컨텍스트 2 (0) | 2022.04.12 |
내부 동작 방식 - 영속성 컨텍스트 1 (0) | 2022.04.12 |