Skip to content

Commit 5795ccf

Browse files
committed
misc cleanups in Binders
1 parent 0a02189 commit 5795ccf

File tree

9 files changed

+367
-305
lines changed

9 files changed

+367
-305
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumns.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,10 @@ public void setJoins(Map<String, Join> joins) {
8383
}
8484

8585
public Join getJoin() {
86-
final AnnotatedColumn firstColumn = columns.get( 0 );
86+
final var firstColumn = columns.get( 0 );
8787
final String explicitTableName = firstColumn.getExplicitTableName();
8888
//note: checkPropertyConsistency() is responsible for ensuring they all have the same table name
89-
Join join = joins.get( explicitTableName );
90-
if ( join == null ) {
91-
// annotation binding seems to use logical and physical naming somewhat inconsistently...
92-
final String physicalTableName = getBuildingContext().getMetadataCollector()
93-
.getPhysicalTableName( explicitTableName );
94-
if ( physicalTableName != null ) {
95-
join = joins.get( physicalTableName );
96-
}
97-
}
89+
final var join = getJoin( explicitTableName );
9890
if ( join == null ) {
9991
throw new AnnotationException(
10092
"Secondary table '" + explicitTableName + "' for property '" + propertyName + "' of entity'" + getPropertyHolder().getClassName()
@@ -106,8 +98,22 @@ public Join getJoin() {
10698
}
10799
}
108100

101+
private Join getJoin(String explicitTableName) {
102+
final var join = joins.get( explicitTableName );
103+
if ( join != null ) {
104+
return join;
105+
}
106+
else {
107+
// annotation binding seems to use logical and physical naming somewhat inconsistently...
108+
final String physicalTableName =
109+
getBuildingContext().getMetadataCollector()
110+
.getPhysicalTableName( explicitTableName );
111+
return physicalTableName != null ? joins.get( physicalTableName ) : null;
112+
}
113+
}
114+
109115
public boolean isSecondary() {
110-
final AnnotatedColumn firstColumn = columns.get( 0 );
116+
final var firstColumn = columns.get( 0 );
111117
final String explicitTableName = firstColumn.getExplicitTableName();
112118
//note: checkPropertyConsistency() is responsible for ensuring they all have the same table name
113119
return isNotEmpty( explicitTableName )

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumns.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@
3737

3838
import jakarta.persistence.JoinColumn;
3939

40+
import static org.hibernate.boot.model.internal.AnnotatedJoinColumn.buildExplicitJoinTableJoinColumn;
41+
import static org.hibernate.boot.model.internal.AnnotatedJoinColumn.buildImplicitJoinTableJoinColumn;
42+
import static org.hibernate.boot.model.internal.AnnotatedJoinColumn.buildJoinColumn;
4043
import static org.hibernate.boot.model.internal.BinderHelper.findReferencedColumnOwner;
4144
import static org.hibernate.boot.model.internal.BinderHelper.getRelativePath;
45+
import static org.hibernate.boot.model.internal.ForeignKeyType.EXPLICIT_PRIMARY_KEY_REFERENCE;
46+
import static org.hibernate.boot.model.internal.ForeignKeyType.NON_PRIMARY_KEY_REFERENCE;
47+
import static org.hibernate.boot.model.naming.ImplicitJoinColumnNameSource.Nature.ELEMENT_COLLECTION;
48+
import static org.hibernate.boot.model.naming.ImplicitJoinColumnNameSource.Nature.ENTITY;
49+
import static org.hibernate.boot.model.naming.ImplicitJoinColumnNameSource.Nature.ENTITY_COLLECTION;
4250
import static org.hibernate.internal.util.StringHelper.isBlank;
4351
import static org.hibernate.internal.util.StringHelper.isNotBlank;
4452
import static org.hibernate.internal.util.StringHelper.isQuoted;
@@ -94,7 +102,7 @@ public static AnnotatedJoinColumns buildJoinColumnsOrFormulas(
94102
AnnotatedJoinColumn.buildJoinFormula( formula, parent );
95103
}
96104
else {
97-
AnnotatedJoinColumn.buildJoinColumn( column, mappedBy, parent, propertyHolder, inferredData );
105+
buildJoinColumn( column, mappedBy, parent, propertyHolder, inferredData );
98106
}
99107
}
100108

@@ -169,7 +177,7 @@ public static AnnotatedJoinColumns buildJoinColumnsWithDefaultColumnSuffix(
169177
parent.setPropertyName( getRelativePath( propertyHolder, propertyName ) );
170178
parent.setMappedBy( mappedBy );
171179
if ( isEmpty( actualColumns ) ) {
172-
AnnotatedJoinColumn.buildJoinColumn(
180+
buildJoinColumn(
173181
null,
174182
mappedBy,
175183
parent,
@@ -181,7 +189,7 @@ public static AnnotatedJoinColumns buildJoinColumnsWithDefaultColumnSuffix(
181189
else {
182190
parent.setMappedBy( mappedBy );
183191
for ( var actualColumn : actualColumns ) {
184-
AnnotatedJoinColumn.buildJoinColumn(
192+
buildJoinColumn(
185193
actualColumn,
186194
mappedBy,
187195
parent,
@@ -212,11 +220,11 @@ public static AnnotatedJoinColumns buildJoinTableJoinColumns(
212220
parent.setPropertyName( getRelativePath( propertyHolder, inferredData.getPropertyName() ) );
213221
parent.setMappedBy( mappedBy );
214222
if ( joinColumns == null ) {
215-
AnnotatedJoinColumn.buildImplicitJoinTableJoinColumn( parent, propertyHolder, inferredData );
223+
buildImplicitJoinTableJoinColumn( parent, propertyHolder, inferredData );
216224
}
217225
else {
218226
for ( var joinColumn : joinColumns ) {
219-
AnnotatedJoinColumn.buildExplicitJoinTableJoinColumn( parent, propertyHolder, inferredData, joinColumn );
227+
buildExplicitJoinTableJoinColumn( parent, propertyHolder, inferredData, joinColumn );
220228
}
221229
}
222230
handlePropertyRef( inferredData.getAttributeMember(), parent );
@@ -228,11 +236,11 @@ Property resolveMapsId() {
228236
final var identifier = persistentClass.getIdentifier();
229237
try {
230238
return identifier instanceof Component embeddedIdType
231-
? embeddedIdType.getProperty( getMapsId() ) // an @EmbeddedId
232-
: persistentClass.getProperty( getMapsId() ); // a simple id or an @IdClass
239+
? embeddedIdType.getProperty( mapsId ) // an @EmbeddedId
240+
: persistentClass.getProperty( mapsId ); // a simple id or an @IdClass
233241
}
234242
catch (MappingException me) {
235-
throw new AnnotationException( "Identifier field '" + getMapsId()
243+
throw new AnnotationException( "Identifier field '" + mapsId
236244
+ "' named in '@MapsId' does not exist in entity '" + persistentClass.getEntityName() + "'",
237245
me );
238246
}
@@ -318,7 +326,7 @@ public void setMappedBy(String entityName, String logicalTableName, String mappe
318326
*/
319327
public ForeignKeyType getReferencedColumnsType(PersistentClass referencedEntity) {
320328
if ( referencedProperty != null ) {
321-
return ForeignKeyType.NON_PRIMARY_KEY_REFERENCE;
329+
return NON_PRIMARY_KEY_REFERENCE;
322330
}
323331

324332
if ( columns.isEmpty() ) {
@@ -341,7 +349,7 @@ public ForeignKeyType getReferencedColumnsType(PersistentClass referencedEntity)
341349
throw new FailedSecondPassException( me.getMessage(), me );
342350
}
343351
}
344-
final Table table = table( columnOwner );
352+
final var table = table( columnOwner );
345353
// final List<Selectable> keyColumns = referencedEntity.getKey().getSelectables();
346354
final var keyColumns =
347355
table.getPrimaryKey() == null
@@ -353,17 +361,17 @@ public ForeignKeyType getReferencedColumnsType(PersistentClass referencedEntity)
353361
explicitColumnReference = true;
354362
if ( !keyColumns.contains( column( context, table, column.getReferencedColumn() ) ) ) {
355363
// we have a column which does not belong to the PK
356-
return ForeignKeyType.NON_PRIMARY_KEY_REFERENCE;
364+
return NON_PRIMARY_KEY_REFERENCE;
357365
}
358366
}
359367
}
360368
if ( explicitColumnReference ) {
361369
// if we got to here, all the columns belong to the PK
362370
return keyColumns.size() == columns.size()
363371
// we have all the PK columns
364-
? ForeignKeyType.EXPLICIT_PRIMARY_KEY_REFERENCE
372+
? EXPLICIT_PRIMARY_KEY_REFERENCE
365373
// we have a subset of the PK columns
366-
: ForeignKeyType.NON_PRIMARY_KEY_REFERENCE;
374+
: NON_PRIMARY_KEY_REFERENCE;
367375
}
368376
else {
369377
// there were no nonempty referencedColumnNames
@@ -395,8 +403,9 @@ private static Column column(MetadataBuildingContext context, Table table, Strin
395403
}
396404

397405
String buildDefaultColumnName(PersistentClass referencedEntity, String logicalReferencedColumn) {
398-
final var options = getBuildingContext().getBuildingOptions();
399-
final var collector = getBuildingContext().getMetadataCollector();
406+
final var context = getBuildingContext();
407+
final var options = context.getBuildingOptions();
408+
final var collector = context.getMetadataCollector();
400409
final var database = collector.getDatabase();
401410
final var jdbcEnvironment = database.getJdbcEnvironment();
402411
final Identifier columnIdentifier = columnIdentifier(
@@ -464,9 +473,10 @@ public Identifier getReferencedPrimaryKeyColumnName() {
464473

465474
private Identifier handleElement(Identifier columnIdentifier) {
466475
// HHH-11826 magic. See AnnotatedColumn and the HHH-6005 comments
467-
if ( columnIdentifier.getText().contains( "_{element}_" ) ) {
476+
final String identifierText = columnIdentifier.getText();
477+
if ( identifierText.contains( "_{element}_" ) ) {
468478
return Identifier.toIdentifier(
469-
columnIdentifier.getText().replace( "_{element}_", "_" ),
479+
identifierText.replace( "_{element}_", "_" ),
470480
columnIdentifier.isQuoted()
471481
);
472482
}
@@ -502,13 +512,13 @@ private boolean isMappedBySide() {
502512

503513
private ImplicitJoinColumnNameSource.Nature getImplicitNature() {
504514
if ( getPropertyHolder().isEntity() ) {
505-
return ImplicitJoinColumnNameSource.Nature.ENTITY;
515+
return ENTITY;
506516
}
507517
else if ( isElementCollection() ) {
508-
return ImplicitJoinColumnNameSource.Nature.ELEMENT_COLLECTION;
518+
return ELEMENT_COLLECTION;
509519
}
510520
else {
511-
return ImplicitJoinColumnNameSource.Nature.ENTITY_COLLECTION;
521+
return ENTITY_COLLECTION;
512522
}
513523
}
514524

@@ -589,7 +599,7 @@ public Identifier getReferencedColumnName() {
589599
return null;
590600
}
591601

592-
final Property mappedByProperty =
602+
final var mappedByProperty =
593603
collector.getEntityBinding( getMappedByEntityName() )
594604
.getProperty( getMappedByPropertyName() );
595605
final var value = (SimpleValue) mappedByProperty.getValue();

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotationBinder.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,15 @@ public static void bindDefaults(MetadataBuildingContext context) {
8282
final var definitionBuilder = new IdentifierGeneratorDefinition.Builder();
8383
interpretSequenceGenerator( generatorRegistration.configuration(), definitionBuilder );
8484
final var idGenDef = definitionBuilder.build();
85-
if ( BOOT_LOGGER.isTraceEnabled() ) {
86-
BOOT_LOGGER.addingGlobalSequenceGenerator( name );
87-
}
85+
BOOT_LOGGER.addingGlobalSequenceGenerator( name );
8886
metadataCollector.addDefaultIdentifierGenerator( idGenDef );
8987
} );
9088

9189
globalRegistrations.getTableGeneratorRegistrations().forEach( (name, generatorRegistration) -> {
9290
final var definitionBuilder = new IdentifierGeneratorDefinition.Builder();
9391
interpretTableGenerator( generatorRegistration.configuration(), definitionBuilder );
9492
final var idGenDef = definitionBuilder.build();
95-
if ( BOOT_LOGGER.isTraceEnabled() ) {
96-
BOOT_LOGGER.addingGlobalTableGenerator( name );
97-
}
93+
BOOT_LOGGER.addingGlobalTableGenerator( name );
9894
metadataCollector.addDefaultIdentifierGenerator( idGenDef );
9995
} );
10096

@@ -277,7 +273,6 @@ private static void bindTypeDescriptorRegistrations(
277273
AnnotationTarget annotatedElement,
278274
MetadataBuildingContext context) {
279275
final var managedBeanRegistry = context.getBootstrapContext().getManagedBeanRegistry();
280-
281276
final var sourceModelContext = modelsContext( context );
282277

283278
annotatedElement.forEachAnnotationUsage( JavaTypeRegistration.class, sourceModelContext, (usage) -> {
@@ -422,9 +417,8 @@ private static void bindFetchProfile(FetchProfile fetchProfile, MetadataBuilding
422417
final String name = fetchProfile.name();
423418
if ( reuseOrCreateFetchProfile( context, name ) ) {
424419
for ( var fetchOverride : fetchProfile.fetchOverrides() ) {
425-
final FetchType fetchType = fetchOverride.fetch();
426-
final FetchMode fetchMode = fetchOverride.mode();
427-
if ( fetchType == FetchType.LAZY && fetchMode == FetchMode.JOIN ) {
420+
if ( fetchOverride.fetch() == FetchType.LAZY
421+
&& fetchOverride.mode() == FetchMode.JOIN ) {
428422
throw new AnnotationException(
429423
"Fetch profile '" + name
430424
+ "' has a '@FetchOverride' with 'fetch=LAZY' and 'mode=JOIN'"
@@ -484,28 +478,29 @@ public static Map<ClassDetails, InheritanceState> buildInheritanceStates(
484478
collector.registerEmbeddableSubclass( superEntityState.getClassDetails(), classDetails );
485479
}
486480
}
487-
logMixedInheritance( classDetails, superclassState, state );
488-
if ( superclassState.getType() != null ) {
489-
state.setType( superclassState.getType() );
481+
checkMixedInheritance( classDetails, superclassState, state );
482+
final var inheritanceType = superclassState.getType();
483+
if ( inheritanceType != null ) {
484+
state.setType( inheritanceType );
490485
}
491486
}
492487
switch ( classType ) {
493-
case ENTITY:
494-
case MAPPED_SUPERCLASS:
495-
case EMBEDDABLE:
488+
case ENTITY, MAPPED_SUPERCLASS, EMBEDDABLE:
496489
inheritanceStatePerClass.put( classDetails, state );
497490
}
498491
}
499492
return inheritanceStatePerClass;
500493
}
501494

502-
private static void logMixedInheritance(ClassDetails classDetails, InheritanceState superclassState, InheritanceState state) {
503-
if ( state.getType() != null && superclassState.getType() != null ) {
504-
final boolean nonDefault = InheritanceType.SINGLE_TABLE != state.getType();
505-
final boolean mixingStrategy = state.getType() != superclassState.getType();
495+
private static void checkMixedInheritance(ClassDetails classDetails, InheritanceState superclassState, InheritanceState state) {
496+
final var inheritanceType = state.getType();
497+
final var superclassInheritanceType = superclassState.getType();
498+
if ( inheritanceType != null && superclassInheritanceType != null ) {
499+
final boolean nonDefault = InheritanceType.SINGLE_TABLE != inheritanceType;
500+
final boolean mixingStrategy = inheritanceType != superclassInheritanceType;
506501
if ( nonDefault && mixingStrategy ) {
507502
throw new AnnotationException( "Entity '" + classDetails.getName()
508-
+ "' may not override the inheritance mapping strategy '" + superclassState.getType()
503+
+ "' may not override the inheritance mapping strategy '" + superclassInheritanceType
509504
+ "' of its hierarchy"
510505
+ "' (each entity hierarchy has a single inheritance mapping strategy)" );
511506
}

0 commit comments

Comments
 (0)