diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumns.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumns.java index fc68fcceae..97eb592cf8 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumns.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumns.java @@ -119,7 +119,7 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { public static AnnotatedJoinColumns buildJoinColumns( JoinColumn[] joinColumns, -// Comment comment, +// Comment comment, String mappedBy, Map joins, PropertyHolder propertyHolder, @@ -127,7 +127,7 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { MetadataBuildingContext buildingContext) { return buildJoinColumnsWithDefaultColumnSuffix( joinColumns, -// comment, +// comment, mappedBy, joins, propertyHolder, @@ -139,7 +139,7 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { public static AnnotatedJoinColumns buildJoinColumnsWithDefaultColumnSuffix( JoinColumn[] joinColumns, -// Comment comment, +// Comment comment, String mappedBy, Map joins, PropertyHolder propertyHolder, @@ -160,7 +160,7 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { if ( actualColumns == null || actualColumns.length == 0 ) { AnnotatedJoinColumn.buildJoinColumn( null, -// comment, +// comment, mappedBy, parent, propertyHolder, @@ -173,7 +173,7 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { for ( JoinColumn actualColumn : actualColumns ) { AnnotatedJoinColumn.buildJoinColumn( actualColumn, -// comment, +// comment, mappedBy, parent, propertyHolder, @@ -260,7 +260,7 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { /** * @return true if the association mapping annotation did specify - * {@link jakarta.persistence.OneToMany#mappedBy() mappedBy}, + * {@link jakarta.persistence.OneToMany#mappedBy() mappedBy}, * meaning that this {@code @JoinColumn} mapping belongs to an * unowned many-valued association. */ @@ -328,7 +328,7 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { } } final Table table = table( columnOwner ); -// final List keyColumns = referencedEntity.getKey().getSelectables(); +// final List keyColumns = referencedEntity.getKey().getSelectables(); final List keyColumns = table.getPrimaryKey() == null ? referencedEntity.getKey().getSelectables() : table.getPrimaryKey().getColumns(); @@ -367,7 +367,7 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { return new Column( context.getMetadataCollector() .getPhysicalColumnName( table, logicalReferencedColumnName ) ); } - catch (MappingException me ) { + catch ( MappingException me ) { throw new MappingException( "No column with logical name '" + logicalReferencedColumnName + "' in table '" + table.getName() + "'" ); } @@ -375,58 +375,58 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { String buildDefaultColumnName(PersistentClass referencedEntity, String logicalReferencedColumn) { final MetadataBuildingOptions options = getBuildingContext().getBuildingOptions(); - final ImplicitNamingStrategy implicitNamingStrategy = options.getImplicitNamingStrategy(); - final PhysicalNamingStrategy physicalNamingStrategy = options.getPhysicalNamingStrategy(); - - boolean mappedBySide = getMappedByTableName() != null || getMappedByPropertyName() != null; - boolean ownerSide = getPropertyName() != null; - boolean isRefColumnQuoted = isQuoted( logicalReferencedColumn ); - final InFlightMetadataCollector collector = getBuildingContext().getMetadataCollector(); final Database database = collector.getDatabase(); + final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment(); + final Identifier columnIdentifier = columnIdentifier( + referencedEntity, + logicalReferencedColumn, + options.getImplicitNamingStrategy(), + collector, + database + ); + return options.getPhysicalNamingStrategy() + .toPhysicalColumnName( columnIdentifier, jdbcEnvironment ) + .render( jdbcEnvironment.getDialect() ); + } - Identifier columnIdentifier; - if ( mappedBySide ) { - // NOTE : While it is completely misleading here to allow for the combination - // of a "JPA ElementCollection" to be mappedBy, the code that uses this - // class relies on this behavior for handling the inverse side of - // many-to-many mappings - columnIdentifier = implicitNamingStrategy.determineJoinColumnName( + private Identifier columnIdentifier( + PersistentClass referencedEntity, + String logicalReferencedColumn, + ImplicitNamingStrategy implicitNamingStrategy, + InFlightMetadataCollector collector, + Database database) { + boolean isRefColumnQuoted = isQuoted( logicalReferencedColumn ); + + if ( isMappedBySide() ) { + // NOTE: An @ElementCollection can't be mappedBy, but the client code + // also handles the inverse side of many-to-many mappings + final Identifier columnIdentifier = implicitNamingStrategy.determineJoinColumnName( new UnownedImplicitJoinColumnNameSource( referencedEntity, logicalReferencedColumn ) ); - //one element was quoted so we quote - if ( isRefColumnQuoted || isQuoted( getMappedByTableName() ) ) { - columnIdentifier = Identifier.quote( columnIdentifier ); - } + return quoteIfNecessary( isRefColumnQuoted, getMappedByTableName(), columnIdentifier ); } - else if ( ownerSide ) { + else if ( isOwnerSide() ) { final String logicalTableName = collector.getLogicalTableName( referencedEntity.getTable() ); - - columnIdentifier = implicitNamingStrategy.determineJoinColumnName( - new OwnedImplicitJoinColumnNameSource( referencedEntity, logicalTableName, logicalReferencedColumn ) + Identifier columnIdentifier = implicitNamingStrategy.determineJoinColumnName( + new OwnedImplicitJoinColumnNameSource(referencedEntity, logicalTableName, logicalReferencedColumn) ); - - // HHH-11826 magic. See Ejb3Column and the HHH-6005 comments + // HHH-11826 magic. See AnnotatedColumn and the HHH-6005 comments if ( columnIdentifier.getText().contains( "_collection&&element_" ) ) { columnIdentifier = Identifier.toIdentifier( columnIdentifier.getText().replace( "_collection&&element_", "_" ), columnIdentifier.isQuoted() ); } - - //one element was quoted so we quote - if ( isRefColumnQuoted || isQuoted( logicalTableName ) ) { - columnIdentifier = Identifier.quote( columnIdentifier ); - } + return quoteIfNecessary( isRefColumnQuoted, logicalTableName, columnIdentifier ); } else { final Identifier logicalTableName = database.toIdentifier( collector.getLogicalTableName( referencedEntity.getTable() ) ); - // is an intra-entity hierarchy table join so copy the name by default - columnIdentifier = implicitNamingStrategy.determinePrimaryKeyJoinColumnName( + final Identifier columnIdentifier = implicitNamingStrategy.determinePrimaryKeyJoinColumnName( new ImplicitPrimaryKeyJoinColumnNameSource() { @Override public MetadataBuildingContext getBuildingContext() { @@ -444,15 +444,32 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { } } ); - - if ( !columnIdentifier.isQuoted() && ( isRefColumnQuoted || logicalTableName.isQuoted() ) ) { - columnIdentifier = Identifier.quote( columnIdentifier ); - } + return quoteIfNecessary( isRefColumnQuoted, logicalTableName, columnIdentifier ); } + } - final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment(); - return physicalNamingStrategy.toPhysicalColumnName( columnIdentifier, jdbcEnvironment ) - .render( jdbcEnvironment.getDialect() ); + private static Identifier quoteIfNecessary( + boolean isRefColumnQuoted, Identifier logicalTableName, Identifier columnIdentifier) { + return !columnIdentifier.isQuoted() && ( isRefColumnQuoted || logicalTableName.isQuoted() ) + ? Identifier.quote( columnIdentifier ) + : columnIdentifier; + } + + private static Identifier quoteIfNecessary( + boolean isRefColumnQuoted, String logicalTableName, Identifier columnIdentifier) { + //one element was quoted so we quote + return isRefColumnQuoted || isQuoted( logicalTableName ) + ? Identifier.quote( columnIdentifier ) + : columnIdentifier; + } + + private boolean isOwnerSide() { + return getPropertyName() != null; + } + + private boolean isMappedBySide() { + return getMappedByTableName() != null + || getMappedByPropertyName() != null; } /** diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/TableBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/TableBinder.java index ab131127a6..f8b4430f6d 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/TableBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/TableBinder.java @@ -310,7 +310,7 @@ public class TableBinder { final Identifier logicalName; if ( isJPA2ElementCollection ) { - logicalName = buildingContext.getBuildingOptions().getImplicitNamingStrategy().determineCollectionTableName( + logicalName = buildingContext.getBuildingOptions().getImplicitNamingStrategy().determineCollectionTableName( new ImplicitCollectionTableNameSource() { private final EntityNaming owningEntityNaming = new EntityNaming() { @Override