Skip to content

Commit 750a9ad

Browse files
committed
cleanups to InFlightMetadataCollector[Impl]
incl attempt to fix PK to FK copying
1 parent cebef16 commit 750a9ad

File tree

5 files changed

+55
-59
lines changed

5 files changed

+55
-59
lines changed

hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,8 @@ public org.hibernate.type.Type getReferencedPropertyType(String entityName, Stri
10201020
}
10211021

10221022

1023-
private Map<Identifier,Identifier> logicalToPhysicalTableNameMap = new HashMap<>();
1024-
private Map<Identifier,Identifier> physicalToLogicalTableNameMap = new HashMap<>();
1023+
private final Map<Identifier,Identifier> logicalToPhysicalTableNameMap = new HashMap<>();
1024+
private final Map<Identifier,Identifier> physicalToLogicalTableNameMap = new HashMap<>();
10251025

10261026
@Override
10271027
public void addTableNameBinding(Identifier logicalName, Table table) {
@@ -1204,7 +1204,7 @@ public String getLogicalColumnName(Table table, Identifier physicalName) throws
12041204
}
12051205
}
12061206

1207-
if ( DenormalizedTable.class.isInstance( currentTable ) ) {
1207+
if ( currentTable instanceof DenormalizedTable ) {
12081208
currentTable = ( (DenormalizedTable) currentTable ).getIncludedTable();
12091209
}
12101210
else {
@@ -1265,15 +1265,15 @@ else if ( clazz.isAnnotationPresent( Imported.class ) ) {
12651265

12661266

12671267
@Override
1268-
public void addMappedSuperclass(Class type, MappedSuperclass mappedSuperclass) {
1268+
public void addMappedSuperclass(Class<?> type, MappedSuperclass mappedSuperclass) {
12691269
if ( mappedSuperClasses == null ) {
12701270
mappedSuperClasses = new HashMap<>();
12711271
}
12721272
mappedSuperClasses.put( type, mappedSuperclass );
12731273
}
12741274

12751275
@Override
1276-
public MappedSuperclass getMappedSuperclass(Class type) {
1276+
public MappedSuperclass getMappedSuperclass(Class<?> type) {
12771277
if ( mappedSuperClasses == null ) {
12781278
return null;
12791279
}
@@ -1421,13 +1421,12 @@ public void addUniquePropertyReference(String referencedClass, String propertyNa
14211421
);
14221422
}
14231423

1424-
@Override
1425-
@SuppressWarnings("unchecked")
1426-
public void addUniqueConstraints(Table table, List uniqueConstraints) {
1424+
@Override @Deprecated(forRemoval = true)
1425+
public void addUniqueConstraints(Table table, List<String[]> uniqueConstraints) {
14271426
List<UniqueConstraintHolder> constraintHolders = new ArrayList<>( uniqueConstraints.size() );
14281427

14291428
int keyNameBase = determineCurrentNumberOfUniqueConstraintHolders( table );
1430-
for ( String[] columns : ( List<String[]> ) uniqueConstraints ) {
1429+
for ( String[] columns : uniqueConstraints ) {
14311430
final String keyName = "key" + keyNameBase++;
14321431
constraintHolders.add(
14331432
new UniqueConstraintHolder().setName( keyName ).setColumns( columns )
@@ -1438,9 +1437,7 @@ public void addUniqueConstraints(Table table, List uniqueConstraints) {
14381437

14391438
private int determineCurrentNumberOfUniqueConstraintHolders(Table table) {
14401439
List currentHolders = uniqueConstraintHoldersByTable == null ? null : uniqueConstraintHoldersByTable.get( table );
1441-
return currentHolders == null
1442-
? 0
1443-
: currentHolders.size();
1440+
return currentHolders == null ? 0 : currentHolders.size();
14441441
}
14451442

14461443
@Override
@@ -1514,7 +1511,7 @@ public Map<String, Join> getJoins(String entityName) {
15141511
private static final class EntityTableXrefImpl implements EntityTableXref {
15151512
private final Identifier primaryTableLogicalName;
15161513
private final Table primaryTable;
1517-
private EntityTableXrefImpl superEntityTableXref;
1514+
private final EntityTableXrefImpl superEntityTableXref;
15181515

15191516
//annotations needs a Map<String,Join>
15201517
//private Map<Identifier,Join> secondaryTableJoinMap;
@@ -1972,6 +1969,7 @@ protected void secondPassCompileForeignKeys(Table table, Set<ForeignKey> done, M
19721969
throws MappingException {
19731970
table.createForeignKeys();
19741971

1972+
final Dialect dialect = getDatabase().getJdbcEnvironment().getDialect();
19751973
for ( ForeignKey foreignKey : table.getForeignKeys().values() ) {
19761974
if ( !done.contains( foreignKey ) ) {
19771975
done.add( foreignKey );
@@ -1998,7 +1996,7 @@ protected void secondPassCompileForeignKeys(Table table, Set<ForeignKey> done, M
19981996

19991997
final Identifier nameIdentifier = getMetadataBuildingOptions().getImplicitNamingStrategy()
20001998
.determineForeignKeyName( new ForeignKeyNameSource( foreignKey, table, buildingContext ) );
2001-
foreignKey.setName( nameIdentifier.render( getDatabase().getJdbcEnvironment().getDialect() ) );
1999+
foreignKey.setName( nameIdentifier.render( dialect ) );
20022000

20032001
foreignKey.alignColumns();
20042002
}
@@ -2017,14 +2015,13 @@ private List<Identifier> toIdentifiers(String[] names) {
20172015
return columnNames;
20182016
}
20192017

2020-
@SuppressWarnings("unchecked")
2021-
private List<Identifier> extractColumnNames(List columns) {
2018+
private List<Identifier> extractColumnNames(List<Column> columns) {
20222019
if ( columns == null || columns.isEmpty() ) {
20232020
return emptyList();
20242021
}
20252022

20262023
final List<Identifier> columnNames = arrayList( columns.size() );
2027-
for ( Column column : (List<Column>) columns ) {
2024+
for ( Column column : columns ) {
20282025
columnNames.add( getDatabase().toIdentifier( column.getQuotedName() ) );
20292026
}
20302027
return columnNames;
@@ -2397,13 +2394,6 @@ private void handleIdentifierValueBinding(
23972394
}
23982395
}
23992396

2400-
private String extractName(Identifier identifier, Dialect dialect) {
2401-
if ( identifier == null ) {
2402-
return null;
2403-
}
2404-
return identifier.render( dialect );
2405-
}
2406-
24072397
private class IndexOrUniqueKeyNameSource implements ImplicitIndexNameSource, ImplicitUniqueKeyNameSource {
24082398
private final MetadataBuildingContext buildingContext;
24092399
private final Table table;

hibernate-core/src/main/java/org/hibernate/boot/spi/InFlightMetadataCollector.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ void addTableNameBinding(
306306
AnnotatedClassType addClassType(XClass clazz);
307307
AnnotatedClassType getClassType(XClass clazz);
308308

309-
void addMappedSuperclass(Class type, MappedSuperclass mappedSuperclass);
310-
MappedSuperclass getMappedSuperclass(Class type);
309+
void addMappedSuperclass(Class<?> type, MappedSuperclass mappedSuperclass);
310+
MappedSuperclass getMappedSuperclass(Class<?> type);
311311

312312
PropertyData getPropertyAnnotatedWithMapsId(XClass persistentXClass, String propertyName);
313313
void addPropertyAnnotatedWithMapsId(XClass entity, PropertyData propertyAnnotatedElement);
@@ -352,7 +352,11 @@ interface DelayedPropertyReferenceHandler extends Serializable {
352352
void addMappedBy(String name, String mappedBy, String propertyName);
353353
String getFromMappedBy(String ownerEntityName, String propertyName);
354354

355-
void addUniqueConstraints(Table table, List uniqueConstraints);
355+
/**
356+
* @deprecated no longer used
357+
*/
358+
@Deprecated(forRemoval = true)
359+
void addUniqueConstraints(Table table, List<String[]> uniqueConstraints);
356360
void addUniqueConstraintHolders(Table table, List<UniqueConstraintHolder> uniqueConstraints);
357361
void addJpaIndexHolders(Table table, List<JPAIndexHolder> jpaIndexHolders);
358362

hibernate-core/src/main/java/org/hibernate/mapping/Component.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.hibernate.type.ComponentType;
4646
import org.hibernate.type.CompositeType;
4747
import org.hibernate.type.EmbeddedComponentType;
48-
import org.hibernate.type.Type;
4948

5049
import static org.hibernate.generator.EventType.INSERT;
5150
import static org.hibernate.id.IdentifierGeneratorHelper.POST_INSERT_INDICATOR;

hibernate-core/src/main/java/org/hibernate/mapping/ForeignKey.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import org.hibernate.annotations.OnDeleteAction;
1515
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
1616
import org.hibernate.dialect.Dialect;
17-
import org.hibernate.internal.util.StringHelper;
17+
18+
import static org.hibernate.internal.util.StringHelper.qualify;
1819

1920
/**
2021
* A mapping model object representing a {@linkplain jakarta.persistence.ForeignKey foreign key} constraint.
@@ -34,8 +35,8 @@ public ForeignKey() {
3435

3536
@Override
3637
public String getExportIdentifier() {
37-
// NOt sure name is always set. Might need some implicit naming
38-
return StringHelper.qualify( getTable().getExportIdentifier(), "FK-" + getName() );
38+
// Not sure name is always set. Might need some implicit naming
39+
return qualify( getTable().getExportIdentifier(), "FK-" + getName() );
3940
}
4041

4142
public void disableCreation() {
@@ -116,32 +117,33 @@ public void setReferencedTable(Table referencedTable) throws MappingException {
116117
*/
117118
public void alignColumns() {
118119
if ( isReferenceToPrimaryKey() ) {
119-
alignColumns( referencedTable );
120-
}
121-
}
122-
123-
private void alignColumns(Table referencedTable) {
124-
final int columnSpan = getColumnSpan();
125-
final PrimaryKey primaryKey = referencedTable.getPrimaryKey();
126-
if ( primaryKey.getColumnSpan() != columnSpan ) {
127-
StringBuilder sb = new StringBuilder();
128-
sb.append( "Foreign key (" ).append( getName() ).append( ":" )
129-
.append( getTable().getName() )
130-
.append( " [" );
131-
appendColumns( sb, getColumns().iterator() );
132-
sb.append( "])" )
133-
.append( ") must have same number of columns as the referenced primary key (" )
134-
.append( referencedTable.getName() )
135-
.append( " [" );
136-
appendColumns( sb, primaryKey.getColumns().iterator() );
137-
sb.append( "])" );
138-
throw new MappingException( sb.toString() );
139-
}
120+
final int columnSpan = getColumnSpan();
121+
final PrimaryKey primaryKey = referencedTable.getPrimaryKey();
122+
if ( primaryKey.getColumnSpan() != columnSpan ) {
123+
StringBuilder sb = new StringBuilder();
124+
sb.append( "Foreign key (" ).append( getName() ).append( ":" )
125+
.append( getTable().getName() )
126+
.append( " [" );
127+
appendColumns( sb, getColumns().iterator() );
128+
sb.append( "])" )
129+
.append( ") must have same number of columns as the referenced primary key (" )
130+
.append( referencedTable.getName() )
131+
.append( " [" );
132+
appendColumns( sb, primaryKey.getColumns().iterator() );
133+
sb.append( "])" );
134+
throw new MappingException( sb.toString() );
135+
}
140136

141-
for (int i = 0; i<columnSpan; i++ ) {
142-
getColumn(i).setLength( primaryKey.getColumn(i).getLength() );
137+
//TODO: shouldn't this happen even for non-PK references?
138+
for ( int i = 0; i<columnSpan; i++ ) {
139+
Column referencedColumn = primaryKey.getColumn(i);
140+
Column referencingColumn = getColumn(i);
141+
referencingColumn.setLength( referencedColumn.getLength() );
142+
referencingColumn.setScale( referencedColumn.getScale() );
143+
referencingColumn.setPrecision( referencedColumn.getPrecision() );
144+
referencingColumn.setSpecializedTypeDeclaration( referencedColumn.getSpecializedTypeDeclaration() );
145+
}
143146
}
144-
145147
}
146148

147149
public String getReferencedEntityName() {
@@ -186,8 +188,8 @@ public void setCascadeDeleteEnabled(boolean cascadeDeleteEnabled) {
186188

187189
public boolean isPhysicalConstraint() {
188190
return referencedTable.isPhysicalTable()
189-
&& getTable().isPhysicalTable()
190-
&& !referencedTable.hasDenormalizedTables();
191+
&& getTable().isPhysicalTable()
192+
&& !referencedTable.hasDenormalizedTables();
191193
}
192194

193195
/**

hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import jakarta.persistence.AttributeConverter;
6565

6666
import static org.hibernate.id.factory.internal.IdentifierGeneratorUtil.createLegacyIdentifierGenerator;
67+
import static org.hibernate.internal.util.collections.ArrayHelper.toBooleanArray;
6768

6869
/**
6970
* A mapping model object that represents any value that maps to columns.
@@ -230,8 +231,8 @@ protected void justAddFormula(Formula formula) {
230231
public void sortColumns(int[] originalOrder) {
231232
if ( columns.size() > 1 ) {
232233
final Selectable[] originalColumns = columns.toArray( new Selectable[0] );
233-
final boolean[] originalInsertability = ArrayHelper.toBooleanArray( insertability );
234-
final boolean[] originalUpdatability = ArrayHelper.toBooleanArray( updatability );
234+
final boolean[] originalInsertability = toBooleanArray( insertability );
235+
final boolean[] originalUpdatability = toBooleanArray( updatability );
235236
for ( int i = 0; i < originalOrder.length; i++ ) {
236237
final int originalIndex = originalOrder[i];
237238
final Selectable selectable = originalColumns[i];

0 commit comments

Comments
 (0)