Back End/Spring Data JPA

[Spring Data JPA] DB 예약어 처리 (You have an error in your SQL syntax)

DevPing9_ 2022. 12. 9. 18:25

 

 

실 DB 에 JPA 를 붙이다보면 가끔 마주하는 현상이다.
에러 메세지도 모호하기 그저 없다.
SQL 문법이 틀렸다, SQL을 실행할 수 없다. 컬럼이 없다. 등등
차라리 Keyword Constraint 에 걸렸다고 해주지....

무튼 모호한 에러메세지라면 한번 쯤 DB Keyword 에 걸리지 않았는지 생각해보자.

 

DB 예약어(Keyword) 를 위반한 엔티티

@Entity
class UserAttendanceEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "INT")
    val id: Long? = null

    @Column(columnDefinition = "INT")
    val userIdx: Long? = null

    @Column(name = "year")
    val year: Int? = null

    @Column(name = "month")
    val month: Int? = null

    @Column(name = "day")
    val day: Int? = null
    val regTime: LocalDateTime? = null
}

 


 

DB 예약어(Keyword) escape

@Entity
class UserAttendanceEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "INT")
    val id: Long? = null

    @Column(columnDefinition = "INT")
    val userIdx: Long? = null

    @Column(name = "\"year\"")
    val year: Int? = null

    @Column(name = "\"month\"")
    val month: Int? = null

    @Column(name = "\"day\"")
    val day: Int? = null
    val regTime: LocalDateTime? = null
}

 


 

SQL 문 Keyword escape

INSERT INTO tapas.t_attendance (id, user_idx, "year", "month", "day", reg_time)
VALUES (1, 159833, 2019, 2, 27, '2018-02-27 09:03:34');

 


 

Hiberante Escape Globally

spring.jpa.properties.hibernate.globally_quoted_identifiers 를 true 로 설정

 

 

 

728x90