DB Column name 고를시 주의점

fart man·2026년 1월 9일

과제를 하면서 이런 Entity를 작성했다.

package com.twodo.todo.entity;

import com.twodo.common.entity.BaseEntity;
import com.twodo.user.entity.User;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "todos")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Todo extends BaseEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "user_id")
  private User user;

  @NotNull @Column() private String title;

  @NotNull @Column() private String body;

  @NotNull
  @Column(nullable = false)
  private LocalDateTime when;

  public Todo(User user, String title, String body, LocalDateTime when) {
    this.user = user;
    this.title = title;
    this.body = body;
    this.when = when;
  }

  public void update(String title, String body, LocalDateTime when) {
    this.title = title;
    this.body = body;
    this.when = when;
  }
}

그리고 돌려본 결과 이런 에러를 뿜었다.

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
    create table todos (
        created_at timestamp(6),
        id bigint generated by default as identity,
        modified_at timestamp(6),
        user_id bigint,
        when timestamp(6) not null,
        body varchar(255) not null,
        title varchar(255) not null,
        primary key (id)
    )" via JDBC [Syntax error in SQL statement "\000d\000a    create table todos (\000d\000a        created_at timestamp(6),\000d\000a        id bigint generated by default as identity,\000d\000a        modified_at timestamp(6),\000d\000a        user_id bigint,\000d\000a        [*]when timestamp(6) not null,\000d\000a        body varchar(255) not null,\000d\000a        title varchar(255) not null,\000d\000a        primary key (id)\000d\000a    )"; expected "identifier";]

뭔 SQL문이 잘못됬다는 소리 같은데, 내가 쓴 것도 아닌데 왜 문법 에러가 나지?

SQL을 잘몰라 구글에 검색해보니

when은 sql 문법자체에 쓰이는데 내가 when을 Column이름으로 쓸려고해서 에러가 났다고 한다....

아니 이름이 when인거 알면 그냥 when쓰지 마세용! 그거 예약어 입니다!라고 JPA에서 알려줘야 되는거 아닌가???

...어쨋든 그래서 이름을 dueDate로 바꿨다.

0개의 댓글