Hibernate 5 命名策略示例

24

Hibernate有一些标准的命名策略:

default
for org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl - an alias for jpa

jpa
for org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl - the JPA 2.0 compliant naming strategy

legacy-hbm
for org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl - compliant with the original Hibernate NamingStrategy

legacy-jpa
for org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl - compliant with the legacy NamingStrategy developed for JPA 1.0, which was unfortunately unclear in many respects regarding implicit naming rules.

component-path
for org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl - mostly follows ImplicitNamingStrategyJpaCompliantImpl rules, except that it uses the full composite paths, as opposed to just the ending property part

但我找不到每种策略的示例。 你有任何想法在哪里可以找到这样的例子吗?


你好,CPA,你需要哪一类的例子呢?策略背后的想法是要遵守底层数据库模型。你需要关于与 Hibernate 代码相关联的数据库架构的例子吗?还是其他什么? - Kineolyan
3
例子有助于理解差异。假设给定相同的实体类,采用不同的命名策略会生成什么样的数据定义语言(DDL)? - JR Utily
@JRUtily,已完成 :) - Anatoly Shamov
您可以在此处找到一些澄清:https://thoughts-on-java.org/naming-strategies-in-hibernate-5/ - orog
1个回答

17

这里是一个例子,展示了这四种命名策略之间的差异(使用 Hibernate 5.2.11.RELEASE)。

Java 类

@Entity
@Table(name = "mainTable")
public class MainEntity {
    @Id
    private Long id;

    @ElementCollection
    Set<EmbeddableElement> mainElements;

    @OneToMany(targetEntity = DependentEntity.class)
    Set<DependentEntity> dependentEntities;

    @OneToOne(targetEntity = OwnedEntity.class)
    OwnedEntity ownedEntity;
}

@Entity
@Table(name = "dependentTable")
public class DependentEntity {
    @Id
    private Long id;

    @ManyToOne
    MainEntity mainEntity;

    @ElementCollection
    @CollectionTable(name = "dependentElements")
    Set<EmbeddableElement> dependentElements;
}

@Entity(name = "`owned_table`")
public class OwnedEntity {
    @Id
    private Long id;

    @ElementCollection
    @CollectionTable
    Set<EmbeddableElement> ownedElements;
}

@Embeddable
public class EmbeddableElement {
    @Column(name = "`quotedField`")
    String quotedField;

    @Column
    String regularField;
}

生成的DDLs

ImplicitNamingStrategyJpaCompliantImpl

默认命名策略。实现ImplicitNamingStrategy接口,通常倾向于符合JPA标准。

create table main_table 
(id bigint not null, "owned_entity_id" bigint, primary key (id))

create table main_entity_main_elements 
(main_entity_id bigint not null, "quoted_field" varchar(255), regular_field varchar(255))

create table main_table_dependent_table 
(main_entity_id bigint not null, dependent_entities_id bigint not null, primary key (main_entity_id, dependent_entities_id))

create table dependent_table 
(id bigint not null, main_entity_id bigint, primary key (id))

create table dependent_elements 
(dependent_entity_id bigint not null, "quoted_field" varchar(255), regular_field varchar(255))

create table "owned_table" 
(id bigint not null, primary key (id))

create table "`owned_table`_owned_elements" 
("`owned_table`_id" bigint not null, "quoted_field" varchar(255), regular_field varchar(255))

ImplicitNamingStrategyLegacyHbmImpl

该类实现了原始的遗留命名行为。

与默认策略相比,其主要区别在于:

  • 实体名称(参见OwnedEntity)
  • 基本列名称(参见MainEntity.ownedEntity)
  • 联接表名称(参见OwnedEntity.ownedElements)
  • 联接列名称(参见MainEntity.dependentEntities)
  • 引号包含的名称(参见MainEntity.ownedEntity、OwnedEntity、OwnedEntity.ownedElements)

create table main_table 
(id bigint not null, owned_entity bigint, primary key (id))

create table main_entity_main_elements 
(main_entity_id bigint not null, "quoted_field" varchar(255), regular_field varchar(255))

create table main_table_dependent_entities 
(main_entity_id bigint not null, dependent_entities bigint not null, primary key (main_entity_id, dependent_entities))

create table dependent_table 
(id bigint not null, main_entity bigint, primary key (id))

create table dependent_elements 
(dependent_entity_id bigint not null, "quoted_field" varchar(255), regular_field varchar(255))

create table owned_entity 
(id bigint not null, primary key (id))

create table owned_entity_owned_elements 
(owned_entity_id bigint not null, "quoted_field" varchar(255), regular_field varchar(255))

ImplicitNamingStrategyLegacyJpaImpl

这是实现了隐式命名策略的类,符合Hibernate最初为JPA 1.0实现的命名规则,此时还有很多事情没有被澄清。

与默认策略相比,其主要区别在于:

  • 连接表名称(请参见MainEntity.dependentEntities)
  • 连接列名称(请参见MainEntity.mainElements)
  • 引用集合表名称(请参见OwnedEntity.ownedElements)

create table main_table 
(id bigint not null, "owned_entity_id" bigint, primary key (id))

create table main_table_main_elements 
(main_table_id bigint not null, "quoted_field" varchar(255), regular_field varchar(255))

create table main_table_dependent_table 
(main_table_id bigint not null, dependent_entities_id bigint not null, primary key (main_table_id, dependent_entities_id))

create table dependent_table 
(id bigint not null, main_entity_id bigint, primary key (id))

create table dependent_elements 
(dependent_table_id bigint not null, "quoted_field" varchar(255), regular_field varchar(255))

create table "owned_table" 
(id bigint not null, primary key (id))

create table "owned_table_owned_elements" 
("owned_table_id" bigint not null, "quoted_field" varchar(255), regular_field varchar(255))

ImplicitNamingStrategyComponentPathImpl

这是一个隐式命名策略的实现,它使用从AttributePath中提取的完整组合路径,而不仅仅是终端属性部分。

与默认策略的主要区别在于:

  • 属性名称(请参见MainEntity.mainElements.regularField)

create table main_table 
(id bigint not null, "owned_entity_id" bigint, primary key (id))

create table main_entity_main_elements 
(main_entity_id bigint not null, "quoted_field" varchar(255), main_elements_regular_field varchar(255))

create table main_table_dependent_table 
(main_entity_id bigint not null, dependent_entities_id bigint not null, primary key (main_entity_id, dependent_entities_id))

create table dependent_table 
(id bigint not null, main_entity_id bigint, primary key (id))

create table dependent_elements 
(dependent_entity_id bigint not null, "quoted_field" varchar(255), dependent_elements_regular_field varchar(255))

create table "owned_table" 
(id bigint not null, primary key (id))

create table "`owned_table`_owned_elements" 
("`owned_table`_id" bigint not null, "quoted_field" varchar(255), owned_elements_regular_field varchar(255))

使用\@Embedded并使用@Column(name='')定义列名存在问题。如果您使用隐式而没有列名属性,则由于重写,它可以正常工作。如果您使用嵌入式,则会强制使用名称,并且不会将嵌入式路径重写到名称上。 - mjs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接