HHH-16592 minor refactor

This commit is contained in:
Gavin King 2023-09-11 10:17:42 +02:00
parent b1116c8b71
commit 38f761daca
2 changed files with 64 additions and 47 deletions

View File

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

View File

@ -310,7 +310,7 @@ public class TableBinder {
final Identifier logicalName; final Identifier logicalName;
if ( isJPA2ElementCollection ) { if ( isJPA2ElementCollection ) {
logicalName = buildingContext.getBuildingOptions().getImplicitNamingStrategy().determineCollectionTableName( logicalName = buildingContext.getBuildingOptions().getImplicitNamingStrategy().determineCollectionTableName(
new ImplicitCollectionTableNameSource() { new ImplicitCollectionTableNameSource() {
private final EntityNaming owningEntityNaming = new EntityNaming() { private final EntityNaming owningEntityNaming = new EntityNaming() {
@Override @Override