[ baeldung ] Hibernate 5 Naming Strategy Configuration
- Hibernate 4
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
- Hibernate 5 에서 두 개의 다른 명명 전략을 사용하는 것으로 변경되었음
- 물리적 구현(PhysicalStrategy)와 논리적 구현(ImplicitStrategy)로 구분
- Overall, the implicit naming strategy relies on default rules provided by Hibernate, while the physical naming strategy allows you to define your own rules for generating the names of database objects.
Implicit Naming Strategy
spring.jpa.hibernate.naming.implicit-strategy
- 얘는 Hibernate가 Java class나 property 이름으로부터 table의 이름과 컬럼 이름을 만들어주는 것을 담당하는 전략임
- default strategy 는
ImplicitNamingStrategyJpaCompliantImpl
이고 이 전략을 사용하면 클래스와 필드의 이름 그대로 table 이름, 컬럼 이름을 만들어줌 - @Table 어노테이션과 @Column 어노테이션에서 name 을 명명해주면 그 이름으로 만들어줌
Physical Naming Strategy
spring.jpa.hibernate.naming.physical-strategy
- 위와 마찬가지로 class, property로 부터 table, column 이름 만들어주는데 차이점을 아직 잘 모르겠음
CamelCaseToUnderscoresNamingStrategy
, PhysicalNamingStrategyStandardImpl
두 가지가 제공되는데 default는 전자
CamelCaseToUnderscoresNamingStrategy
가 사용되면 @Column과 @Table의 name 그대로 만들지 않고 그 name들도 underScore로 바꾸려는 시도를 함(아래에 결과 첨부)
@Table(name = "MDSceneInfoTable") public class SceneInfo { @Id @Column(name = "intID") private Integer id; @Enumerated @Column(name = "roomType", nullable = false) private RoomType roomType; @Column(name = "sceneID", nullable = false) private int sceneId; @Column(name = "actorCapacity", nullable = false) private int maxUserCnt; } // sql CREATE TABLE mdscene_info_table ( intid int NOT NULL, room_type int NOT NULL, sceneid int NOT NULL, actor_capacity int NOT NULL, CONSTRAINT PK__md_scene__3213E83F2EF5396D PRIMARY KEY (intid)
PhysicalNamingStrategyStandardImpl
사용되면 @Column, @Table의 name 필드 동작함
Custom Naming Convention
[ baeldung ] Custom Naming Convention with Spring Data JPA
public class UpperCaseNamingStrategy extends SpringPhysicalNamingStrategy { @Override protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) { return new Identifier(name.toUpperCase(), quoted); } }