Repository 쿼리메소드와 변수명

프로젝트

목록 보기
25/39

문제 상황 : Repository에서 find해올 때 에러가 떴다.

➡︎➡︎ unable to locate attribute with the the given name ~~~...

문제가 생긴 엔티티는 Order(주문테이블)였고, 그 안에 oNum(PK)가 있었는데,
findByUserIdAndONum 으로 하니까 계속 오류가 떴다.
원인을 알 수 없어서 이것저것 코드를 고쳐보다가 구글링을 했다.

문제 원인

➡︎➡︎ 답은 oNum과 같은 앞에 소문자로 시작 + 그다음 바로 대문자 섞인 변수는 JPA의 문제로 쿼리메소드로 작성할 때 제대로 찾아오지 못한다. 변수명을 바꾸던지 @Query문을 작성해야한다고 한다.

그런데..🧐

  • Order를 찾아올 때 orderDetail과 Delivery를 같이 꺼내오기 위해서 양방향으로 연결해놨는데, 계속 @Query를 써버리면... Entity들을 양방향으로 연결한 의미가 없다.
    다른 팀원들에게도 물어보니, 같은 문제를 겪고 있었어서 팀원들과 조율하여 문제가 되는 컬럼 변수명들을 변경하기로 했다. oNumorderNum 와 같이 쿼리메소드에서 문제가 없도록 변경했다.

➡︎➡︎ 전에는 이런 경험이 없어서 실수했지만, 이번 실수를 통해서 다음 프로젝트 또는 입사해서 모두 변경하는 불상사가 일어나지 않도록 처음부터 변수명을 잘 정해야겠다. :)


  • 발생했던 에러메시지 내용 : org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderController' defined in file [/Users/seli/Desktop/myomiFinal/MyoMiBackend/target/classes/com/myomi/order/control/OrderController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderService' defined in file [/Users/seli/Desktop/myomiFinal/MyoMiBackend/target/classes/com/myomi/order/service/OrderService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderRepository' defined in com.myomi.order.repository.OrderRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract com.myomi.order.entity.Order com.myomi.order.repository.OrderRepository.findByUserIdAndONum(java.lang.String,java.lang.Long); Reason: Failed to create query for method public abstract com.myomi.order.entity.Order com.myomi.order.repository.OrderRepository.findByUserIdAndONum(java.lang.String,java.lang.Long)! Unable to locate Attribute with the the given name [ONum] on this ManagedType [com.myomi.order.entity.Order]; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.myomi.order.entity.Order com.myomi.order.repository.OrderRepository.findByUserIdAndONum(java.lang.String,java.lang.Long)! Unable to locate Attribute with the the given name [ONum] on this ManagedType [com.myomi.order.entity.Order]
profile
백엔드를 공부하고 있습니다.

0개의 댓글