|
6 | 6 |
|
7 | 7 | import java.io.Serializable;
|
8 | 8 | import java.util.ArrayList;
|
9 |
| -import java.util.Collections; |
10 | 9 | import java.util.HashSet;
|
11 | 10 | import java.util.List;
|
12 | 11 | import java.util.Map;
|
|
62 | 61 | import org.hibernate.query.sqm.function.SqmFunctionRegistry;
|
63 | 62 | import org.hibernate.service.spi.ServiceRegistryImplementor;
|
64 | 63 | import org.hibernate.tool.schema.Action;
|
65 |
| -import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator; |
| 64 | +import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.ActionGrouping; |
66 | 65 | import org.hibernate.type.spi.TypeConfiguration;
|
67 | 66 |
|
68 | 67 | import static java.lang.String.join;
|
| 68 | +import static java.util.Collections.emptySet; |
69 | 69 | import static org.hibernate.cfg.AvailableSettings.EVENT_LISTENER_PREFIX;
|
70 | 70 | import static org.hibernate.internal.util.StringHelper.splitAtCommas;
|
71 | 71 | import static org.hibernate.internal.util.collections.CollectionHelper.mapOfSize;
|
@@ -164,10 +164,10 @@ public SqmFunctionRegistry getFunctionRegistry() {
|
164 | 164 |
|
165 | 165 | @Override
|
166 | 166 | public SessionFactoryBuilder getSessionFactoryBuilder() {
|
167 |
| - final SessionFactoryBuilderImplementor defaultBuilder = getFactoryBuilder(); |
| 167 | + final var defaultBuilder = getFactoryBuilder(); |
168 | 168 | SessionFactoryBuilder builder = null;
|
169 | 169 | List<String> activeFactoryNames = null;
|
170 |
| - for ( SessionFactoryBuilderFactory discoveredBuilderFactory : getSessionFactoryBuilderFactories() ) { |
| 170 | + for ( var discoveredBuilderFactory : getSessionFactoryBuilderFactories() ) { |
171 | 171 | final SessionFactoryBuilder returnedBuilder =
|
172 | 172 | discoveredBuilderFactory.getSessionFactoryBuilder( this, defaultBuilder );
|
173 | 173 | if ( returnedBuilder != null ) {
|
@@ -371,85 +371,90 @@ public NamedObjectRepository buildNamedQueryRepository() {
|
371 | 371 | @Override
|
372 | 372 | public void orderColumns(boolean forceOrdering) {
|
373 | 373 | final ColumnOrderingStrategy columnOrderingStrategy = metadataBuildingOptions.getColumnOrderingStrategy();
|
374 |
| - if ( columnOrderingStrategy == ColumnOrderingStrategyLegacy.INSTANCE ) { |
375 |
| - // No need to order columns when using the no-op strategy |
376 |
| - return; |
| 374 | + // No need to order columns when using the no-op strategy |
| 375 | + if ( columnOrderingStrategy != ColumnOrderingStrategyLegacy.INSTANCE ) { |
| 376 | + final boolean shouldOrderTableColumns = forceOrdering || shouldOrderTableColumns(); |
| 377 | + for ( Namespace namespace : database.getNamespaces() ) { |
| 378 | + if ( shouldOrderTableColumns ) { |
| 379 | + for ( Table table : namespace.getTables() ) { |
| 380 | + handleTable( table, columnOrderingStrategy ); |
| 381 | + handlePrimaryKey( table, columnOrderingStrategy ); |
| 382 | + handleForeignKeys( table, columnOrderingStrategy ); |
| 383 | + } |
| 384 | + } |
| 385 | + for ( UserDefinedType userDefinedType : namespace.getUserDefinedTypes() ) { |
| 386 | + handleUDT( userDefinedType, columnOrderingStrategy ); |
| 387 | + } |
| 388 | + } |
377 | 389 | }
|
| 390 | + } |
378 | 391 |
|
379 |
| - final boolean shouldOrderTableColumns = forceOrdering || shouldOrderTableColumns(); |
| 392 | + private void handleTable(Table table, ColumnOrderingStrategy columnOrderingStrategy) { |
| 393 | + final List<Column> tableColumns = columnOrderingStrategy.orderTableColumns( table, this ); |
| 394 | + if ( tableColumns != null ) { |
| 395 | + table.reorderColumns( tableColumns ); |
| 396 | + } |
| 397 | + } |
380 | 398 |
|
381 |
| - for ( Namespace namespace : database.getNamespaces() ) { |
382 |
| - if ( shouldOrderTableColumns ) { |
383 |
| - for ( Table table : namespace.getTables() ) { |
384 |
| - final List<Column> tableColumns = columnOrderingStrategy.orderTableColumns( table, this ); |
385 |
| - if ( tableColumns != null ) { |
386 |
| - table.reorderColumns( tableColumns ); |
387 |
| - } |
388 |
| - final PrimaryKey primaryKey = table.getPrimaryKey(); |
389 |
| - if ( primaryKey != null && primaryKey.getColumns() |
390 |
| - .size() > 1 && primaryKey.getOriginalOrder() == null ) { |
391 |
| - final List<Column> primaryKeyColumns = columnOrderingStrategy.orderConstraintColumns( |
392 |
| - primaryKey, |
393 |
| - this |
394 |
| - ); |
| 399 | + private void handleUDT(UserDefinedType userDefinedType, ColumnOrderingStrategy columnOrderingStrategy) { |
| 400 | + if ( userDefinedType instanceof UserDefinedObjectType objectType |
| 401 | + && objectType.getColumns().size() > 1 ) { |
| 402 | + final List<Column> objectTypeColumns = |
| 403 | + columnOrderingStrategy.orderUserDefinedTypeColumns( objectType, this ); |
| 404 | + if ( objectTypeColumns != null ) { |
| 405 | + objectType.reorderColumns( objectTypeColumns ); |
| 406 | + } |
| 407 | + } |
| 408 | + } |
| 409 | + |
| 410 | + private void handleForeignKeys(Table table, ColumnOrderingStrategy columnOrderingStrategy) { |
| 411 | + for ( ForeignKey foreignKey : table.getForeignKeys().values() ) { |
| 412 | + final List<Column> columns = foreignKey.getColumns(); |
| 413 | + if ( columns.size() > 1 ) { |
| 414 | + if ( foreignKey.getReferencedColumns().isEmpty() ) { |
| 415 | + final PrimaryKey targetPrimaryKey = |
| 416 | + foreignKey.getReferencedTable().getPrimaryKey(); |
| 417 | + // Make sure we order the columns of the primary key first, |
| 418 | + // so that foreign key ordering can rely on this |
| 419 | + if ( targetPrimaryKey.getOriginalOrder() == null ) { |
| 420 | + final List<Column> primaryKeyColumns = |
| 421 | + columnOrderingStrategy.orderConstraintColumns( targetPrimaryKey, this ); |
395 | 422 | if ( primaryKeyColumns != null ) {
|
396 |
| - primaryKey.reorderColumns( primaryKeyColumns ); |
| 423 | + targetPrimaryKey.reorderColumns( primaryKeyColumns ); |
397 | 424 | }
|
398 | 425 | }
|
399 |
| - for ( ForeignKey foreignKey : table.getForeignKeys().values() ) { |
400 |
| - final List<Column> columns = foreignKey.getColumns(); |
401 |
| - if ( columns.size() > 1 ) { |
402 |
| - if ( foreignKey.getReferencedColumns().isEmpty() ) { |
403 |
| - final PrimaryKey foreignKeyTargetPrimaryKey = foreignKey.getReferencedTable() |
404 |
| - .getPrimaryKey(); |
405 |
| - // Make sure we order the columns of the primary key first, |
406 |
| - // so that foreign key ordering can rely on this |
407 |
| - if ( foreignKeyTargetPrimaryKey.getOriginalOrder() == null ) { |
408 |
| - final List<Column> primaryKeyColumns = columnOrderingStrategy.orderConstraintColumns( |
409 |
| - foreignKeyTargetPrimaryKey, |
410 |
| - this |
411 |
| - ); |
412 |
| - if ( primaryKeyColumns != null ) { |
413 |
| - foreignKeyTargetPrimaryKey.reorderColumns( primaryKeyColumns ); |
414 |
| - } |
415 |
| - } |
416 |
| - |
417 |
| - // Patch up the order of foreign keys based on new order of the target primary key |
418 |
| - final int[] originalPrimaryKeyOrder = foreignKeyTargetPrimaryKey.getOriginalOrder(); |
419 |
| - if ( originalPrimaryKeyOrder != null ) { |
420 |
| - final ArrayList<Column> foreignKeyColumnsCopy = new ArrayList<>( columns ); |
421 |
| - for ( int i = 0; i < foreignKeyColumnsCopy.size(); i++ ) { |
422 |
| - columns.set( i, foreignKeyColumnsCopy.get( originalPrimaryKeyOrder[i] ) ); |
423 |
| - } |
424 |
| - } |
425 |
| - } |
| 426 | + |
| 427 | + // Patch up the order of foreign keys based on new order of the target primary key |
| 428 | + final int[] originalPrimaryKeyOrder = targetPrimaryKey.getOriginalOrder(); |
| 429 | + if ( originalPrimaryKeyOrder != null ) { |
| 430 | + final ArrayList<Column> foreignKeyColumnsCopy = new ArrayList<>( columns ); |
| 431 | + for ( int i = 0; i < foreignKeyColumnsCopy.size(); i++ ) { |
| 432 | + columns.set( i, foreignKeyColumnsCopy.get( originalPrimaryKeyOrder[i] ) ); |
426 | 433 | }
|
427 | 434 | }
|
428 | 435 | }
|
429 | 436 | }
|
430 |
| - for ( UserDefinedType userDefinedType : namespace.getUserDefinedTypes() ) { |
431 |
| - if ( userDefinedType instanceof UserDefinedObjectType objectType |
432 |
| - && objectType.getColumns().size() > 1 ) { |
433 |
| - final List<Column> objectTypeColumns = |
434 |
| - columnOrderingStrategy.orderUserDefinedTypeColumns( objectType, this ); |
435 |
| - if ( objectTypeColumns != null ) { |
436 |
| - objectType.reorderColumns( objectTypeColumns ); |
437 |
| - } |
438 |
| - } |
| 437 | + } |
| 438 | + } |
| 439 | + |
| 440 | + private void handlePrimaryKey(Table table, ColumnOrderingStrategy columnOrderingStrategy) { |
| 441 | + final PrimaryKey primaryKey = table.getPrimaryKey(); |
| 442 | + if ( primaryKey != null && primaryKey.getColumns() |
| 443 | + .size() > 1 && primaryKey.getOriginalOrder() == null ) { |
| 444 | + final List<Column> primaryKeyColumns = |
| 445 | + columnOrderingStrategy.orderConstraintColumns( primaryKey, this ); |
| 446 | + if ( primaryKeyColumns != null ) { |
| 447 | + primaryKey.reorderColumns( primaryKeyColumns ); |
439 | 448 | }
|
440 | 449 | }
|
441 | 450 | }
|
442 | 451 |
|
443 | 452 | private boolean shouldOrderTableColumns() {
|
444 |
| - final ConfigurationService configurationService = |
445 |
| - metadataBuildingOptions.getServiceRegistry().requireService( ConfigurationService.class ); |
446 |
| - final Set<SchemaManagementToolCoordinator.ActionGrouping> groupings = |
447 |
| - SchemaManagementToolCoordinator.ActionGrouping.interpret( this, |
448 |
| - configurationService.getSettings() ); |
449 |
| - if ( groupings.isEmpty() ) { |
450 |
| - return false; |
451 |
| - } |
452 |
| - for ( SchemaManagementToolCoordinator.ActionGrouping grouping : groupings ) { |
| 453 | + final var settings = |
| 454 | + metadataBuildingOptions.getServiceRegistry() |
| 455 | + .requireService( ConfigurationService.class ) |
| 456 | + .getSettings(); |
| 457 | + for ( ActionGrouping grouping : ActionGrouping.interpret( this, settings ) ) { |
453 | 458 | if ( isColumnOrderingRelevant( grouping.getScriptAction() )
|
454 | 459 | || isColumnOrderingRelevant( grouping.getDatabaseAction() ) ) {
|
455 | 460 | return true;
|
@@ -479,30 +484,29 @@ public void validate() throws MappingException {
|
479 | 484 | @Override
|
480 | 485 | public Set<MappedSuperclass> getMappedSuperclassMappingsCopy() {
|
481 | 486 | return mappedSuperclassMap == null
|
482 |
| - ? Collections.emptySet() |
| 487 | + ? emptySet() |
483 | 488 | : new HashSet<>( mappedSuperclassMap.values() );
|
484 | 489 | }
|
485 | 490 |
|
486 | 491 | @Override
|
487 | 492 | public void initSessionFactory(SessionFactoryImplementor sessionFactory) {
|
488 | 493 | // must not use BootstrapContext services here
|
489 |
| - final ServiceRegistryImplementor sessionFactoryServiceRegistry = sessionFactory.getServiceRegistry(); |
490 |
| - assert sessionFactoryServiceRegistry != null; |
| 494 | + final ServiceRegistryImplementor registry = sessionFactory.getServiceRegistry(); |
| 495 | + assert registry != null; |
491 | 496 | final ConfigurationService configurationService =
|
492 |
| - sessionFactoryServiceRegistry.requireService( ConfigurationService.class ); |
| 497 | + registry.requireService( ConfigurationService.class ); |
493 | 498 | final ClassLoaderService classLoaderService =
|
494 |
| - sessionFactoryServiceRegistry.requireService( ClassLoaderService.class ); |
| 499 | + registry.requireService( ClassLoaderService.class ); |
495 | 500 | final EventListenerRegistry eventListenerRegistry =
|
496 |
| - sessionFactoryServiceRegistry.requireService( EventListenerRegistry.class ); |
497 |
| - for ( Map.Entry<String,Object> entry : configurationService.getSettings().entrySet() ) { |
498 |
| - final String propertyName = entry.getKey(); |
| 501 | + registry.requireService( EventListenerRegistry.class ); |
| 502 | + configurationService.getSettings().forEach( (propertyName, value) -> { |
499 | 503 | if ( propertyName.startsWith( EVENT_LISTENER_PREFIX ) ) {
|
500 | 504 | final String eventTypeName = propertyName.substring( EVENT_LISTENER_PREFIX.length() + 1 );
|
501 | 505 | final EventType<?> eventType = EventType.resolveEventTypeByName( eventTypeName );
|
502 |
| - final String listeners = (String) entry.getValue(); |
| 506 | + final String listeners = (String) value; |
503 | 507 | appendListeners( eventListenerRegistry, classLoaderService, listeners, eventType );
|
504 | 508 | }
|
505 |
| - } |
| 509 | + } ); |
506 | 510 | }
|
507 | 511 |
|
508 | 512 | private <T> void appendListeners(
|
|
0 commit comments