Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 5 naming strategy examples

Tags:

Hibernate has some standard naming strategies implemented:

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 

But I can't find any examples for each strategy. Do you have any idea where I can find such examples?

like image 375
CPA Avatar asked Dec 21 '16 16:12

CPA


People also ask

What is naming strategy in hibernate?

Hibernate uses the Physical Naming Strategy to map our logical names to a SQL table and its columns. By default, the physical name will be the same as the logical name that we specified in the previous section. If we want to customize the physical names, we can create a custom PhysicalNamingStrategy class.

What is ImprovedNamingStrategy?

ImprovedNamingStrategy , which will convert the mixed case names to the embedded underscores name .


1 Answers

Here is an example, which shows differences between these four naming strategies (used Hibernate 5.2.11.RELEASE).

Java classes

@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; } 

Generated DDLs

ImplicitNamingStrategyJpaCompliantImpl

Default naming strategy. Implementation of the ImplicitNamingStrategy contract, generally preferring to conform to JPA standards.

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

Implements the original legacy naming behavior.

The main differences with the default strategy are:

  • Entity names (see OwnedEntity)
  • Basic column names (see MainEntity.ownedEntity)
  • Join table names (see OwnedEntity.ownedElements)
  • Join column names (see MainEntity.dependentEntities)
  • Quoted names (see 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

Implementation of the ImplicitNamingStrategy contract which conforms to the naming rules initially implemented by Hibernate for JPA 1.0, prior to many things being clarified.

The main differences with the default strategy are:

  • Join table names (see MainEntity.dependentEntities)
  • Join column names (see MainEntity.mainElements)
  • Quoted collection table names (see 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

An ImplicitNamingStrategy implementation which uses full composite paths extracted from AttributePath, as opposed to just the terminal property part.

The main difference with the default strategy is:

  • Attribute names (see 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)) 
like image 63
Anatoly Shamov Avatar answered Sep 19 '22 21:09

Anatoly Shamov