Spring Boot에서 Entity UUID를 VARCHAR로 저장하는 방법

LeeYulhee·2024년 7월 14일

👉 발생했던 문제


  • DB에서 테이블 생성할 때, user_id의 데이터 타입을 VARCHAR로 설정
    CREATE TABLE `users` (
        `user_id` BINARY(16) NOT NULL,
        `email` VARCHAR(255) NOT NULL,
        `password` VARCHAR(255) NOT NULL,
        PRIMARY KEY (`user_id`)
    );
  • Entity에는 user_id의 참조 자료형을 UUID로 설정
    @Getter
    @Entity
    @Table(name = "users")
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor(access = PROTECTED)
    @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)
    public class User extends SoftDeleteBaseEntity {
    
        @Id
        @EqualsAndHashCode.Include
        @Column(name = "user_id")
        private UUID uuid;
        
        @Column(name = "email")
        private String email;
        
        @Column(name = "password")
        private String password;
    }
  • 프로젝트를 실행시키니 아래와 같은 오류 발생
    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.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [user_id] in table [users]; found [varchar (Types#VARCHAR)], but expecting [binary(16) (Types#BINARY)]
    • users 테이블의 user_id 컬럼에 대해 기대한 자료형은 Binary(16)인데 아니라서 뜨는 오류



👉 해결을 위한 시도


  • @Type 어노테이션을 사용해서 설정 시도
    @Getter
    @Entity
    @Table(name = "users")
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor(access = PROTECTED)
    @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)
    public class User extends SoftDeleteBaseEntity {
    
        @Id
        @EqualsAndHashCode.Include
        @Column(name = "user_id")
        @Type(type = "org.hibernate.type.UUIDCharType") // UUID를 VARCHAR 형태로 저장
        private UUID uuid;
        
        @Column(name = "email")
        private String email;
        
        @Column(name = "password")
        private String password;
    }
    
    • ⇒ @Type(type) 설정이 불가(3.0에서 deprecated로 보였음)

  • users의 user_id 컬럼을 Binary(16)로 변경
    • 이런 식으로 깨져서 저장됨



👉 해결 방법


  • @JdbcTypeCode(SqlTypes.VARCHAR) 로 타입 지정
    @Getter
    @Entity
    @Table(name = "users")
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor(access = PROTECTED)
    @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)
    public class User extends SoftDeleteBaseEntity {
    
        @Id
        @EqualsAndHashCode.Include
        @UuidGenerator(style = RANDOM)
        @JdbcTypeCode(SqlTypes.VARCHAR)
        @Column(name = "user_id", columnDefinition = "VARCHAR(36)")
        private UUID uuid;
        
        @Column(name = "email")
        private String email;
        
        @Column(name = "password")
        private String password;
    }
profile
끝없이 성장하고자 하는 백엔드 개발자입니다.

0개의 댓글