Spring

Spring boot Mysql 삽질

challnum 2023. 1. 18. 20:02

소스 코드

package com.example.assignment.Domain;

import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Table(name = "members")
public class Member {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 30)
    private String username;

    @Column(nullable = false,length = 100)
    private String password;

    @Column(nullable = false,length = 50)
    private String email;

    @ColumnDefault("user_role")
    private String role;
    @CreatedDate
    private LocalDateTime localDateTime;
}

오류 구문

Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'user_role,

하루종일 해결을 해보려고 한 Entity 생성에 대한 답이 @ColumnDefault("user_role") 구문 때문이었다. 혹시나 mysql의 예약어가 포함된건가 하여 aaaa로도 하고 ddqs같은 전혀 연관성 없는 이름을 넣었음에도 오류가 발생하였다.결론적으로는 어노테이션을 잘못 사용한 탓에 발생한 오류였고 해당 오류를 수정하니 아래와 같이 Table이 잘 생성되었다.

null