프레임워크/Spring

[Spring] CrudRepository findById가 안될 때

pythaac 2022. 1. 4. 19:20

현상

  CrudRepository의 메서드를 사용할 때 데이터를 불러오지 못하는 상황이 발생했습니다. 원인을 찾기 위해 확인해본 내용은 아래와 같습니다.

  • DB에 데이터가 제대로 들어갔는지?
    - Mysql Workbench를 통해 table 데이터 확인
  • DAO의 String과 DB table의 varchar가 호환이 되는지?
    - 호환 가능
  • findById의 arg가 정상인지?
    - 코드와 같은 SQL 사용시 데이터 조회 가능

 

해결

  Hibernate 로그를 살펴보니, SQL문의 table 이름에 문제가 있다는 것을 확인했습니다. DB의 table 이름이 TistoryInfo인데, 로그에 따르면 tistory_info라는 table에서 데이터를 찾고 있었습니다.

 

이러한 문제는 아래 글에서 Camel 방식이 underscore로 나누어 인식하게 만든다는 것을 알 수 있었습니다.

https://stackoverflow.com/questions/29087626/entity-class-name-is-transformed-into-sql-table-name-with-underscores

 

Entity Class name is transformed into SQL table name with underscores

I have the following entity defined: @Entity @Table(name = "EmailTemplate") public class EmailTemplate { Despite the table annotation, I receive java.sql.SQLException: Invalid object name '

stackoverflow.com

 

따라서 안전성을 높이기 위해 table의 이름에 underscore를 추가하게 되었습니다.