logo example java hibernate

example - java hibernate xml mapping



Hibernate 5 ejemplos de estrategia de nombres (1)

Hibernate tiene algunas estrategias de nomenclatura estándar implementadas:

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

Pero no puedo encontrar ningún ejemplo para cada estrategia. ¿Tienes alguna idea de dónde puedo encontrar esos ejemplos?


Aquí hay un ejemplo, que muestra las diferencias entre estas cuatro estrategias de denominación (se utiliza Hibernate 5.2.11.RELEASE ).

Clases de 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 generados

ImplícitoNamingStrategyJpaCompliantImpl

Estrategia de nomenclatura por defecto. Implementación del contrato ImplicitNamingStrategy , generalmente prefiriendo cumplir con los estándares 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))

ImplícitoNamingStrategyLegacyHbmImpl

Implementa el comportamiento de los nombres heredados originales.

Las principales diferencias con la estrategia por defecto son:

  • Nombres de entidades (ver OwnedEntity)
  • Nombres de columna básicos (ver MainEntity.ownedEntity)
  • Unir nombres de tablas (ver OwnedEntity.ownedElements)
  • Unir los nombres de columna (ver MainEntity.dependentEntities)
  • Nombres citados (vea 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))

ImplícitoNamingStrategyLegacyJpaImpl

Implementación del contrato ImplicitNamingStrategy que se ajusta a las reglas de denominación inicialmente implementadas por Hibernate para JPA 1.0, antes de que se aclaren muchas cosas.

Las principales diferencias con la estrategia por defecto son:

  • Unir los nombres de tabla (ver MainEntity.dependentEntities)
  • Unir los nombres de columna (ver MainEntity.mainElements)
  • Cita los nombres de la tabla de colección (ver 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

Una implementación ImplicitNamingStrategy que utiliza rutas compuestas completas extraídas de AttributePath, en lugar de solo la parte de la propiedad del terminal.

La principal diferencia con la estrategia por defecto es:

  • Nombres de atributos (ver 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))