트러블 슈팅 - Column 'id' is duplicated in mapping for entity

진용완·2025년 5월 19일

Spring트러블슈팅

목록 보기
2/4

이 포스팅은 신선영 저, 『스프링 부트 3 백엔드 개발자 되기』(골든래빗, 2023)를 공부하면서 마주한 에러 및 버그를 해결하는 과정을 기록하기 위해서 작성되었습니다.


   '8장. 스프링 시큐리티로 로그인/로그아웃, 회원가입 구현하기'에서 로그인, 로그아웃 기능을 구현하는 중, 다음과 같은 컴파일 에러를 마주하게 되었다.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Column 'id' is duplicated in mapping for entity 'MyCompany.MyBlog.domain.User' (use '@Column(insertable=false, updatable=false)' when mapping multiple properties to the same column)

   회원의 정보가 담긴 엔티티 User를 생성할 때에 id 행이 중복되었고, 같은 행에 다수의 속성이 매핑되었다는 의미 같았다. 엔티티 User의 코드를 다시 한번 살펴보았다.

가설 1. 한 엔티티에서 id라고 이름 붙여진 곳이 두 곳 이상이다.

   코드를 다시 살펴보니, 참 어이 없는 실수였다.

@Table(name="users")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Entity
public class User implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false)
    private Long id;

    @Column(name = "id", nullable = false, unique = true)
    private String email;

    @Column(name = "password")
    private String password;

    @Builder
    public User (String email, String password, String auth){
        this.email = email;
        this.password = password;
    }

// ... 생략 ... //

}

   id와 이메일 정보가 담긴 행 모두 이름이 id로 되어있었다. 이름은 같은데 속성이 다른 행을 만드려고 하니 컴파일 에러가 발생했던 것이다.
  다음과 같이 코드를 수정했다.

@Table(name="users")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Entity
public class User implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false)
    private Long id;

    @Column(name = "email", nullable = false, unique = true)
    private String email;

    @Column(name = "password")
    private String password;

    @Builder
    public User (String email, String password, String auth){
        this.email = email;
        this.password = password;
    }

// ... 생략 ... //

}

   코드를 수정하고 나니 컴파일 에러 없이 서버가 정상적으로 실행되었다.

0개의 댓글