-
2.3.1 Create Todo 구현 (오류 해결)기타 2022. 10. 28. 15:03
실행 중 오류 발생
antlr.NoViableAltException: unexpected token: *
중략
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'todoController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'todoService': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'todoRepository' defined in com.example.demo.persistence.TodoRepository 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 java.util.List com.example.demo.persistence.TodoRepository.findByUserId(java.lang.String); Reason: Validation failed for query for method public abstract java.util.List com.example.demo.persistence.TodoRepository.findByUserId(java.lang.String)!; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.example.demo.persistence.TodoRepository.findByUserId(java.lang.String)!
문구를 보니 대충 TodoRepository에 있는 쿼리문에 * 이게 문제가 되는 것 같아서
JpaRepository Query를 구글링해서 쿼리문을 찾아보니
책에 나오는 코드인 아래 코드와는 다른 부분이 있어 수정을 해보았다
@Repository public interface TodoRepository extends JpaRepository<TodoEntity, String> { // ?1은 메서드의 매개변수의 순서 위치 @Query("select * from Todo t where t.userId = ?1") List<TodoEntity> findByUserId(String userId); }
아래와 같이 value와 nativeQuery를 지정해주었더니 오류가 발생하지 않고 실행되었다
@Repository public interface TodoRepository extends JpaRepository<TodoEntity, String> { // ?1은 메서드의 매개변수의 순서 위치 @Query(value = "select * from Todo t where t.userId = ?1", nativeQuery = true) List<TodoEntity> findByUserId(String userId); }
+추가
value와 nativeQuery 지정 후 main 메소드는 실행이 되는데
postman으로 http post 요청을 하면 오류가 난다
postman에서는 아래와 같이 오류 문구가 뜨고
could not prepare statement; SQL [select * from Todo t where t.userId = ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statementeclipse에서는 아래와 같이 로그가 뜬다
2022-10-31 15:04:18.321 WARN 1332 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 42122, SQLState: 42S22
2022-10-31 15:04:18.321 ERROR 1332 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Column "T.USERID" not found; SQL statement:구글링 해보니 nativeQuery를 사용하면 SQL문법을 사용해야 한다고 하는데
일단 다시 코드를 뜯어봐야할 것 같다
+추가
책의 소스 코드와 비교해본 결과 책에 적혀있는 코드와 다른 부분을 발견했고
@Repository public interface TodoRepository extends JpaRepository<TodoEntity, String> { List<TodoEntity> findByUserId(String userId); // ?1은 메서드의 매개변수의 순서 위치 @Query("SELECT t FROM TodoEntity t WHERE t.userId = ?1") TodoEntity findByUserIdQuery(String userId); }
최종 코드는 위와 같다
userId를 못찾을 때 id로 바꿔서 실행을 해봤는데 그러면 오류는 나지 않지만 넘어오는 data가 하나도 없었다
그래서 from todo가 맞는 건지 의문이 들었는데.. 원본 소스코드를 보니 from TodoEntity였다
위의 코드로 했더니 nativeQuery를 설정할 필요도 없었다
책으로 공부하는 건 좋은데 코드에 오류가 있거나 하면 바로 수정할 수 없고
소스코드를 찾아봐야하니까.. 이건 좀 불편한 것 같다
spring JAP docs
Spring Data JPA - Reference Documentation
Example 109. Using @Transactional at query methods @Transactional(readOnly = true) interface UserRepository extends JpaRepository { List findByLastname(String lastname); @Modifying @Transactional @Query("delete from User u where u.active = false") void del
docs.spring.io
오류를 스스로 해결하지 못하면 책의 소스코드를 보며 할 예정
https://github.com/fsoftwareengineer/todo-application-revision2
GitHub - fsoftwareengineer/todo-application-revision2
Contribute to fsoftwareengineer/todo-application-revision2 development by creating an account on GitHub.
github.com
'기타' 카테고리의 다른 글
git bash 오류 해결법 모음 (0) 2022.11.09 2.1 백엔드 개발 환경 설정 (0) 2022.04.28 [DB] SQL 제약조건 (0) 2021.11.19 [VMware 오류] Error While powering on: This host supports AMD-V, but AMD-V is disabled. (0) 2021.09.09