java - Hibernate 5 命名策略示例

标签 java hibernate

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

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

最佳答案

这里是一个例子,展示了这四种命名策略之间的区别(使用了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;
}

生成的 DDL

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

ImplicitNamingStrategy 契约的实现符合 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

一个 ImplicitNamingStrategy 实现,它使用从 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))

关于java - Hibernate 5 命名策略示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41267416/

相关文章:

java - Hibernate 与额外列的多对多关系

java - 计数语句在 Hibernate 中不起作用

java - Spring MVC Maven 模块

java - 为什么在java中与pip行的字符串连接不返回任何内容(空字符串)?

Java:拆箱整数时出现空指针异常?

java - Hibernate 和 JPA 的好的继承策略是什么?

java - Android xml 属性以编程方式

java - 从 5.2.12 升级到 hibernate 5.3.4 后,Hibernate ColumnTransformer 停止工作

java - 使用 Hibernate 和 MySQL 时出现多个错误

hibernate - 如何在 hibernate 中识别对象是 transient 的还是分离的?