Skip to content

Commit fd0c1ba

Browse files
committed
cleanups to OneToManyPersister
Signed-off-by: Gavin King <[email protected]>
1 parent cdaee09 commit fd0c1ba

File tree

1 file changed

+65
-100
lines changed

1 file changed

+65
-100
lines changed

hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java

Lines changed: 65 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
import org.hibernate.persister.collection.mutation.UpdateRowsCoordinatorNoOp;
5252
import org.hibernate.persister.collection.mutation.UpdateRowsCoordinatorOneToMany;
5353
import org.hibernate.persister.collection.mutation.UpdateRowsCoordinatorTablePerSubclass;
54+
import org.hibernate.persister.entity.EntityPersister;
5455
import org.hibernate.persister.entity.UnionSubclassEntityPersister;
55-
import org.hibernate.sql.ast.SqlAstTranslator;
56+
import org.hibernate.service.spi.ServiceRegistryImplementor;
5657
import org.hibernate.sql.ast.spi.SqlAstCreationState;
5758
import org.hibernate.sql.ast.tree.expression.ColumnReference;
5859
import org.hibernate.sql.ast.tree.from.TableGroup;
@@ -79,8 +80,8 @@
7980
import static org.hibernate.sql.model.ast.builder.TableUpdateBuilder.NULL;
8081

8182
/**
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}.
8485
*
8586
* @see BasicCollectionPersister
8687
*
@@ -407,101 +408,81 @@ private RowMutationOperations buildRowMutationOperations() {
407408
private InsertRowsCoordinator buildInsertCoordinator() {
408409
if ( isInverse() || !isRowInsertEnabled() ) {
409410
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() );
414412
}
415413
return new InsertRowsCoordinatorNoOp( this );
416414
}
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 );
421422
}
422-
return new InsertRowsCoordinatorStandard( this, rowMutationOperations, getFactory().getServiceRegistry() );
423423
}
424424

425425
private UpdateRowsCoordinator buildUpdateCoordinator() {
426426
if ( !isRowDeleteEnabled() && !isRowInsertEnabled() ) {
427427
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() );
432429
}
433430
return new UpdateRowsCoordinatorNoOp( this );
434431
}
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() );
439438
}
440-
return new UpdateRowsCoordinatorOneToMany( this, getRowMutationOperations(), getFactory() );
441439
}
442440

443441
private DeleteRowsCoordinator buildDeleteCoordinator() {
444442
if ( !needsRemove() ) {
445443
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() );
450445
}
451446
return new DeleteRowsCoordinatorNoOp( this );
452447
}
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 );
458456
}
459-
return new DeleteRowsCoordinatorStandard(
460-
this,
461-
rowMutationOperations,
462-
// never delete by index for one-to-many
463-
false,
464-
getFactory().getServiceRegistry()
465-
);
466457
}
467458

468459
private RemoveCoordinator buildDeleteAllCoordinator() {
469460
if ( ! needsRemove() ) {
470461
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() );
475463
}
476464
return new RemoveCoordinatorNoOp( this );
477465
}
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 );
482473
}
483-
return new RemoveCoordinatorStandard( this, this::buildDeleteAllOperation, getFactory().getServiceRegistry() );
484474
}
485475

486476
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 );
495480
}
496481

497482
public RestrictedTableMutation<JdbcMutationOperation> generateDeleteRowAst(MutatingTableReference tableReference) {
498483
// 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 );
505486

506487
// for each key column -
507488
// 1) set the value to null
@@ -510,42 +491,36 @@ public RestrictedTableMutation<JdbcMutationOperation> generateDeleteRowAst(Mutat
510491
final int keyTypeCount = keyDescriptor.getJdbcTypeCount();
511492
for ( int i = 0; i < keyTypeCount; i++ ) {
512493
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 );
525506
}
526-
527-
// restrict
528-
updateBuilder.addKeyRestrictionLeniently( selectable );
529507
}
530508

531509
// set the value for each index column to null
532510
if ( hasIndex() && !indexContainsFormula ) {
533511
final CollectionPart indexDescriptor = getAttributeMapping().getIndexDescriptor();
534512
assert indexDescriptor != null;
535-
536513
final int indexTypeCount = indexDescriptor.getJdbcTypeCount();
537514
for ( int i = 0; i < indexTypeCount; i++ ) {
538515
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+
);
541523
}
542-
543-
updateBuilder.addValueColumn(
544-
selectable.getSelectionExpression(),
545-
NULL,
546-
selectable.getJdbcMapping(),
547-
selectable.isLob()
548-
);
549524
}
550525
}
551526

@@ -593,12 +568,8 @@ private JdbcMutationOperation generateInsertRowOperation(MutatingTableReference
593568
}
594569

595570
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 );
602573

603574
final PluralAttributeMapping attributeMapping = getAttributeMapping();
604575
attributeMapping.getKeyDescriptor().getKeyPart().forEachSelectable( updateBuilder );
@@ -667,12 +638,8 @@ private void applyInsertRowValues(
667638

668639
private JdbcMutationOperation generateWriteIndexOperation(MutatingTableReference tableReference) {
669640
// 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 );
676643

677644
final OneToManyCollectionPart elementDescriptor = (OneToManyCollectionPart) getAttributeMapping().getElementDescriptor();
678645
updateBuilder.addKeyRestrictionsLeniently( elementDescriptor.getAssociatedEntityMappingType().getIdentifierMapping() );
@@ -698,10 +665,8 @@ private void applyWriteIndexValues(
698665
int entryPosition,
699666
SharedSessionContractImplementor session,
700667
JdbcValueBindings jdbcValueBindings) {
701-
final Object index = collection.getIndex( entry, entryPosition, this );
702-
703668
getAttributeMapping().getIndexDescriptor().decompose(
704-
index,
669+
collection.getIndex( entry, entryPosition, this ),
705670
0,
706671
jdbcValueBindings,
707672
null,

0 commit comments

Comments
 (0)