51
51
import org .hibernate .persister .collection .mutation .UpdateRowsCoordinatorNoOp ;
52
52
import org .hibernate .persister .collection .mutation .UpdateRowsCoordinatorOneToMany ;
53
53
import org .hibernate .persister .collection .mutation .UpdateRowsCoordinatorTablePerSubclass ;
54
+ import org .hibernate .persister .entity .EntityPersister ;
54
55
import org .hibernate .persister .entity .UnionSubclassEntityPersister ;
55
- import org .hibernate .sql . ast . SqlAstTranslator ;
56
+ import org .hibernate .service . spi . ServiceRegistryImplementor ;
56
57
import org .hibernate .sql .ast .spi .SqlAstCreationState ;
57
58
import org .hibernate .sql .ast .tree .expression .ColumnReference ;
58
59
import org .hibernate .sql .ast .tree .from .TableGroup ;
79
80
import static org .hibernate .sql .model .ast .builder .TableUpdateBuilder .NULL ;
80
81
81
82
/**
82
- * A {@link CollectionPersister} for {@link jakarta.persistence.OneToMany one-to-one
83
- * associations}.
83
+ * A {@link CollectionPersister} for {@linkplain jakarta.persistence.OneToMany
84
+ * one-to-many associations}.
84
85
*
85
86
* @see BasicCollectionPersister
86
87
*
@@ -407,101 +408,81 @@ private RowMutationOperations buildRowMutationOperations() {
407
408
private InsertRowsCoordinator buildInsertCoordinator () {
408
409
if ( isInverse () || !isRowInsertEnabled () ) {
409
410
if ( MODEL_MUTATION_LOGGER .isDebugEnabled () ) {
410
- MODEL_MUTATION_LOGGER .debugf (
411
- "Skipping collection (re)creation - %s" ,
412
- getRolePath ()
413
- );
411
+ MODEL_MUTATION_LOGGER .debugf ( "Skipping collection (re)creation - %s" , getRolePath () );
414
412
}
415
413
return new InsertRowsCoordinatorNoOp ( this );
416
414
}
417
-
418
- if ( getElementPersisterInternal () != null && getElementPersisterInternal ().hasSubclasses ()
419
- && getElementPersisterInternal () instanceof UnionSubclassEntityPersister ) {
420
- return new InsertRowsCoordinatorTablePerSubclass ( this , rowMutationOperations , getFactory ().getServiceRegistry () );
415
+ else {
416
+ final ServiceRegistryImplementor serviceRegistry = getFactory ().getServiceRegistry ();
417
+ final EntityPersister elementPersister = getElementPersisterInternal ();
418
+ return elementPersister != null && elementPersister .hasSubclasses ()
419
+ && elementPersister instanceof UnionSubclassEntityPersister
420
+ ? new InsertRowsCoordinatorTablePerSubclass ( this , rowMutationOperations , serviceRegistry )
421
+ : new InsertRowsCoordinatorStandard ( this , rowMutationOperations , serviceRegistry );
421
422
}
422
- return new InsertRowsCoordinatorStandard ( this , rowMutationOperations , getFactory ().getServiceRegistry () );
423
423
}
424
424
425
425
private UpdateRowsCoordinator buildUpdateCoordinator () {
426
426
if ( !isRowDeleteEnabled () && !isRowInsertEnabled () ) {
427
427
if ( MODEL_MUTATION_LOGGER .isDebugEnabled () ) {
428
- MODEL_MUTATION_LOGGER .debugf (
429
- "Skipping collection row updates - %s" ,
430
- getRolePath ()
431
- );
428
+ MODEL_MUTATION_LOGGER .debugf ( "Skipping collection row updates - %s" , getRolePath () );
432
429
}
433
430
return new UpdateRowsCoordinatorNoOp ( this );
434
431
}
435
-
436
- if ( getElementPersisterInternal () != null && getElementPersisterInternal ().hasSubclasses ()
437
- && getElementPersisterInternal () instanceof UnionSubclassEntityPersister ) {
438
- return new UpdateRowsCoordinatorTablePerSubclass ( this , rowMutationOperations , getFactory () );
432
+ else {
433
+ final EntityPersister elementPersister = getElementPersisterInternal ();
434
+ return elementPersister != null && elementPersister .hasSubclasses ()
435
+ && elementPersister instanceof UnionSubclassEntityPersister
436
+ ? new UpdateRowsCoordinatorTablePerSubclass ( this , rowMutationOperations , getFactory () )
437
+ : new UpdateRowsCoordinatorOneToMany ( this , rowMutationOperations , getFactory () );
439
438
}
440
- return new UpdateRowsCoordinatorOneToMany ( this , getRowMutationOperations (), getFactory () );
441
439
}
442
440
443
441
private DeleteRowsCoordinator buildDeleteCoordinator () {
444
442
if ( !needsRemove () ) {
445
443
if ( MODEL_MUTATION_LOGGER .isDebugEnabled () ) {
446
- MODEL_MUTATION_LOGGER .debugf (
447
- "Skipping collection row deletions - %s" ,
448
- getRolePath ()
449
- );
444
+ MODEL_MUTATION_LOGGER .debugf ( "Skipping collection row deletions - %s" , getRolePath () );
450
445
}
451
446
return new DeleteRowsCoordinatorNoOp ( this );
452
447
}
453
-
454
-
455
- if ( getElementPersisterInternal () != null && getElementPersisterInternal ().hasSubclasses ()
456
- && getElementPersisterInternal () instanceof UnionSubclassEntityPersister ) {
457
- return new DeleteRowsCoordinatorTablePerSubclass ( this , rowMutationOperations , false , getFactory ().getServiceRegistry () );
448
+ else {
449
+ final EntityPersister elementPersister = getElementPersisterInternal ();
450
+ final ServiceRegistryImplementor serviceRegistry = getFactory ().getServiceRegistry ();
451
+ return elementPersister != null && elementPersister .hasSubclasses ()
452
+ && elementPersister instanceof UnionSubclassEntityPersister
453
+ // never delete by index for one-to-many
454
+ ? new DeleteRowsCoordinatorTablePerSubclass ( this , rowMutationOperations , false , serviceRegistry )
455
+ : new DeleteRowsCoordinatorStandard ( this , rowMutationOperations , false , serviceRegistry );
458
456
}
459
- return new DeleteRowsCoordinatorStandard (
460
- this ,
461
- rowMutationOperations ,
462
- // never delete by index for one-to-many
463
- false ,
464
- getFactory ().getServiceRegistry ()
465
- );
466
457
}
467
458
468
459
private RemoveCoordinator buildDeleteAllCoordinator () {
469
460
if ( ! needsRemove () ) {
470
461
if ( MODEL_MUTATION_LOGGER .isDebugEnabled () ) {
471
- MODEL_MUTATION_LOGGER .debugf (
472
- "Skipping collection removals - %s" ,
473
- getRolePath ()
474
- );
462
+ MODEL_MUTATION_LOGGER .debugf ( "Skipping collection removals - %s" , getRolePath () );
475
463
}
476
464
return new RemoveCoordinatorNoOp ( this );
477
465
}
478
-
479
- if ( getElementPersisterInternal () != null && getElementPersisterInternal ().hasSubclasses ()
480
- && getElementPersisterInternal () instanceof UnionSubclassEntityPersister ) {
481
- return new RemoveCoordinatorTablePerSubclass ( this , this ::buildDeleteAllOperation , getFactory ().getServiceRegistry () );
466
+ else {
467
+ final ServiceRegistryImplementor serviceRegistry = getFactory ().getServiceRegistry ();
468
+ final EntityPersister elementPersister = getElementPersisterInternal ();
469
+ return elementPersister != null && elementPersister .hasSubclasses ()
470
+ && elementPersister instanceof UnionSubclassEntityPersister
471
+ ? new RemoveCoordinatorTablePerSubclass ( this , this ::buildDeleteAllOperation , serviceRegistry )
472
+ : new RemoveCoordinatorStandard ( this , this ::buildDeleteAllOperation , serviceRegistry );
482
473
}
483
- return new RemoveCoordinatorStandard ( this , this ::buildDeleteAllOperation , getFactory ().getServiceRegistry () );
484
474
}
485
475
486
476
private JdbcMutationOperation generateDeleteRowOperation (MutatingTableReference tableReference ) {
487
- final RestrictedTableMutation <JdbcMutationOperation > sqlAst = generateDeleteRowAst ( tableReference );
488
-
489
- final SqlAstTranslator <JdbcMutationOperation > translator = getFactory ().getJdbcServices ()
490
- .getDialect ()
491
- .getSqlAstTranslatorFactory ()
492
- .buildModelMutationTranslator ( sqlAst , getFactory () );
493
-
494
- return translator .translate ( null , MutationQueryOptions .INSTANCE );
477
+ return getFactory ().getJdbcServices ().getDialect ().getSqlAstTranslatorFactory ()
478
+ .buildModelMutationTranslator ( generateDeleteRowAst ( tableReference ), getFactory () )
479
+ .translate ( null , MutationQueryOptions .INSTANCE );
495
480
}
496
481
497
482
public RestrictedTableMutation <JdbcMutationOperation > generateDeleteRowAst (MutatingTableReference tableReference ) {
498
483
// note that custom sql delete row details are handled by CollectionRowUpdateBuilder
499
- final CollectionRowDeleteByUpdateSetNullBuilder <MutationOperation > updateBuilder = new CollectionRowDeleteByUpdateSetNullBuilder <>(
500
- this ,
501
- tableReference ,
502
- getFactory (),
503
- sqlWhereString
504
- );
484
+ final CollectionRowDeleteByUpdateSetNullBuilder <MutationOperation > updateBuilder =
485
+ new CollectionRowDeleteByUpdateSetNullBuilder <>( this , tableReference , getFactory (), sqlWhereString );
505
486
506
487
// for each key column -
507
488
// 1) set the value to null
@@ -510,42 +491,36 @@ public RestrictedTableMutation<JdbcMutationOperation> generateDeleteRowAst(Mutat
510
491
final int keyTypeCount = keyDescriptor .getJdbcTypeCount ();
511
492
for ( int i = 0 ; i < keyTypeCount ; i ++ ) {
512
493
final SelectableMapping selectable = keyDescriptor .getSelectable ( i );
513
- if ( selectable .isFormula () ) {
514
- continue ;
515
- }
516
-
517
- if ( selectable .isUpdateable () ) {
518
- // set null
519
- updateBuilder . addValueColumn (
520
- selectable .getSelectionExpression (),
521
- NULL ,
522
- selectable . getJdbcMapping (),
523
- selectable . isLob ()
524
- );
494
+ if ( ! selectable .isFormula () ) {
495
+ if ( selectable . isUpdateable () ) {
496
+ // set null
497
+ updateBuilder . addValueColumn (
498
+ selectable .getSelectionExpression (),
499
+ NULL ,
500
+ selectable . getJdbcMapping (),
501
+ selectable .isLob ()
502
+ );
503
+ }
504
+ // restrict
505
+ updateBuilder . addKeyRestrictionLeniently ( selectable );
525
506
}
526
-
527
- // restrict
528
- updateBuilder .addKeyRestrictionLeniently ( selectable );
529
507
}
530
508
531
509
// set the value for each index column to null
532
510
if ( hasIndex () && !indexContainsFormula ) {
533
511
final CollectionPart indexDescriptor = getAttributeMapping ().getIndexDescriptor ();
534
512
assert indexDescriptor != null ;
535
-
536
513
final int indexTypeCount = indexDescriptor .getJdbcTypeCount ();
537
514
for ( int i = 0 ; i < indexTypeCount ; i ++ ) {
538
515
final SelectableMapping selectable = indexDescriptor .getSelectable ( i );
539
- if ( !selectable .isUpdateable () ) {
540
- continue ;
516
+ if ( selectable .isUpdateable () ) {
517
+ updateBuilder .addValueColumn (
518
+ selectable .getSelectionExpression (),
519
+ NULL ,
520
+ selectable .getJdbcMapping (),
521
+ selectable .isLob ()
522
+ );
541
523
}
542
-
543
- updateBuilder .addValueColumn (
544
- selectable .getSelectionExpression (),
545
- NULL ,
546
- selectable .getJdbcMapping (),
547
- selectable .isLob ()
548
- );
549
524
}
550
525
}
551
526
@@ -593,12 +568,8 @@ private JdbcMutationOperation generateInsertRowOperation(MutatingTableReference
593
568
}
594
569
595
570
private TableUpdate <JdbcMutationOperation > buildTableUpdate (MutatingTableReference tableReference ) {
596
- final TableUpdateBuilderStandard <JdbcMutationOperation > updateBuilder = new TableUpdateBuilderStandard <>(
597
- this ,
598
- tableReference ,
599
- getFactory (),
600
- sqlWhereString
601
- );
571
+ final TableUpdateBuilderStandard <JdbcMutationOperation > updateBuilder =
572
+ new TableUpdateBuilderStandard <>( this , tableReference , getFactory (), sqlWhereString );
602
573
603
574
final PluralAttributeMapping attributeMapping = getAttributeMapping ();
604
575
attributeMapping .getKeyDescriptor ().getKeyPart ().forEachSelectable ( updateBuilder );
@@ -667,12 +638,8 @@ private void applyInsertRowValues(
667
638
668
639
private JdbcMutationOperation generateWriteIndexOperation (MutatingTableReference tableReference ) {
669
640
// note that custom sql update details are handled by TableUpdateBuilderStandard
670
- final TableUpdateBuilderStandard <JdbcMutationOperation > updateBuilder = new TableUpdateBuilderStandard <>(
671
- this ,
672
- tableReference ,
673
- getFactory (),
674
- sqlWhereString
675
- );
641
+ final TableUpdateBuilderStandard <JdbcMutationOperation > updateBuilder =
642
+ new TableUpdateBuilderStandard <>( this , tableReference , getFactory (), sqlWhereString );
676
643
677
644
final OneToManyCollectionPart elementDescriptor = (OneToManyCollectionPart ) getAttributeMapping ().getElementDescriptor ();
678
645
updateBuilder .addKeyRestrictionsLeniently ( elementDescriptor .getAssociatedEntityMappingType ().getIdentifierMapping () );
@@ -698,10 +665,8 @@ private void applyWriteIndexValues(
698
665
int entryPosition ,
699
666
SharedSessionContractImplementor session ,
700
667
JdbcValueBindings jdbcValueBindings ) {
701
- final Object index = collection .getIndex ( entry , entryPosition , this );
702
-
703
668
getAttributeMapping ().getIndexDescriptor ().decompose (
704
- index ,
669
+ collection . getIndex ( entry , entryPosition , this ) ,
705
670
0 ,
706
671
jdbcValueBindings ,
707
672
null ,
0 commit comments