JPA 공부를 하던 와중 사용자 조회를 하려고 할 때 아래와 같은 에러를 만났다.
java.sql.SQLSyntaxErrorException: Unknown column 'user0_.reg_date' in 'field list'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:121) ~[mysql-connector-j-8.0.33.jar:8.0.33]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-j-8.0.33.jar:8.0.33]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916) ~[mysql-connector-j-8.0.33.jar:8.0.33]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:972) ~[mysql-connector-j-8.0.33.jar:8.0.33]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[HikariCP-4.0.3.jar:na]
에러문구를 보니 컬럼 명(reg_date)가 내가 의도한대로 정상적으로 매핑이 안되고 있는 것 같아서 application.yaml에서 아래와 같이 설정을 변경 한 뒤 확인을 해본 결과
spring:
jpa:
show-sql: true
실제로 사용자를 조회할 때 아래와 같은 쿼리가 날려지고 있었다.
Hibernate:
select user0_.uid as uid1_0_, user0_.id as id2_0_, user0_.name as name3_0_, user0_.password as password4_0_, user0_.reg_date as reg_date5_0_, user0_.role as role6_0_
from user user0_
where user0_.id=?
하지만 여기서 문제가 나는 사용자 DTO와 Entity, 그리고 디비에서 컬럼명도 모두 regDate라고 camel case를 사용하였지만 실제로 jpa는 snake case를 규칙으로 사용하고 있어서 정상적인 컬럼 명 매핑이 되지 않았었다.
그래서 아래와 같은 설정을 추가하고 다시 조회를 했더니,
(참고로 아래 설정의 뜻은 엔티티에서 정해놓은 변수명을 그대로 사용한다는 의미이다.)
spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Hibernate:
select user0_.uid as uid1_0_, user0_.id as id2_0_, user0_.name as name3_0_, user0_.password as password4_0_, user0_.regDate as regdate5_0_, user0_.role as role6_0_
from user user0_
where user0_.id=?
정상적으로 컬럼명이 regDate가 들어가면서 조회가 되었다.