본문 바로가기
java

jpa에서 mysql8 예약어로 된 컬럼 사용시 옵션 설정

by 후린트 2024. 2. 5.
반응형

mysql5.7 버전을 사용하다가 myslq8로 마이그레이션 진행시 
기존에 사용하던 컬럼이 myslq8 예약어와 동일한 이름으로 되어 있어 변경이 필요하다.

1. 예약어가 정의된 컬럼에 쌍따옴표 또는 아포스트로피를 추가한다.

@Column(name = "`rank`")
private int rank;

 

하지만 개발중에 mysql에 정의된 예약어와 겹치는 부분에 대해서 항상 체킹해야 하므로 불편하다.
persistence.xml에 property를 추가한다.

<persistence-unit name="commonType" transaction-type="RESOURCE_LOCAL">
	<provider></provider>
    ...
    ...
    
	<properties>
    ...
	  <property name="hibernate.globally_quoted_identifiers" value="true"/>
	  <property name="hibernate.globally_quoted_identifiers_skip_column_definitions" value="true"/>
	...
	</properties>
</persistence-unit>


* Should all database identifiers be quoted. A true/false option.
- hibernate.globally_quoted_identifiers 

* JPA states that column-definitions are subject to global quoting, so by default this setting is false for JPA compliance. Set to true to avoid column-definitions being quoted due to global quoting (they will still be quoted if explicitly quoted in the annotation/xml).
- hibernate.globally_quoted_identifiers_skip_column_definitions

-- property 설정 전
    create table tag (
       id bigint not null auto_increment,
        rank integer,
        tag_count integer not null,
        delete_user_type enum('A','B','C'),
        primary key (id)
    ) engine=InnoDB;

-- hibernate.globally_quoted_identifiers  // true 
-- hibernate.globally_quoted_identifiers_skip_column_definitions // false

    create table `tag` (
       `id` bigint not null auto_increment,
       `rank` integer,
       `tag_count` integer not null,
       `delete_user_type` `enum('A','B','C')`,
       primary key (`id`)
    ) engine=InnoDB;

-- hibernate.globally_quoted_identifiers  // true 
-- hibernate.globally_quoted_identifiers_skip_column_definitions // true

    create table `tag` (
       `id` bigint not null auto_increment,
       `rank` integer,
       `tag_count` integer not null,
       `delete_user_type` enum('A','B','C'),
       primary key (`id`)
    ) engine=InnoDB;

 

참고 : https://github.com/raycon/til/issues/63

https://stackoverflow.com/questions/2224503/how-to-map-an-entity-field-whose-name-is-a-reserved-word-in-jpa

반응형