HHH-16900 Optimise implementations of MutationOperationGroup

This commit is contained in:
Sanne Grinovero 2023-07-04 21:01:50 +01:00 committed by Sanne Grinovero
parent 7dc2449870
commit 9c7194519d
30 changed files with 426 additions and 287 deletions

View File

@ -0,0 +1,26 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.engine.jdbc.mutation.internal;
import org.hibernate.persister.entity.mutation.EntityMutationTarget;
import org.hibernate.sql.model.MutationOperationGroup;
public interface EntityMutationOperationGroup extends MutationOperationGroup {
/**
* The model-part being mutated.
* N.B. it returns a widened type compared to the same method in the super interface.
*/
@Override
EntityMutationTarget getMutationTarget();
@Override
default EntityMutationOperationGroup asEntityMutationOperationGroup() {
return this;
}
}

View File

@ -60,8 +60,8 @@ public class MutationExecutorPostInsert implements MutationExecutor, JdbcValueBi
protected final JdbcValueBindingsImpl valueBindings;
public MutationExecutorPostInsert(MutationOperationGroup mutationOperationGroup, SharedSessionContractImplementor session) {
this.mutationTarget = (EntityMutationTarget) mutationOperationGroup.getMutationTarget();
public MutationExecutorPostInsert(EntityMutationOperationGroup mutationOperationGroup, SharedSessionContractImplementor session) {
this.mutationTarget = mutationOperationGroup.getMutationTarget();
this.valueBindings = new JdbcValueBindingsImpl(
MutationType.INSERT,
mutationTarget,

View File

@ -51,9 +51,9 @@ public class MutationExecutorPostInsertSingleTable implements MutationExecutor,
private final JdbcValueBindingsImpl valueBindings;
public MutationExecutorPostInsertSingleTable(
MutationOperationGroup mutationOperationGroup,
EntityMutationOperationGroup mutationOperationGroup,
SharedSessionContractImplementor session) {
this.mutationTarget = (EntityMutationTarget) mutationOperationGroup.getMutationTarget();
this.mutationTarget = mutationOperationGroup.getMutationTarget();
this.session = session;
assert mutationOperationGroup.getNumberOfOperations() == 1;

View File

@ -7,7 +7,6 @@
package org.hibernate.engine.jdbc.mutation.internal;
import java.util.Map;
import java.util.function.Supplier;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.batch.spi.BatchKey;
@ -16,11 +15,8 @@ import org.hibernate.engine.jdbc.mutation.spi.BatchKeyAccess;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.persister.entity.mutation.EntityMutationTarget;
import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationTarget;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.PreparableMutationOperation;
import org.hibernate.sql.model.SelfExecutingUpdateOperation;
@ -58,18 +54,15 @@ public class StandardMutationExecutorService implements MutationExecutorService
final int numberOfOperations = operationGroup.getNumberOfOperations();
final MutationType mutationType = operationGroup.getMutationType();
final MutationTarget<?> mutationTarget = operationGroup.getMutationTarget();
final EntityMutationOperationGroup entityMutationOperationGroup = operationGroup.asEntityMutationOperationGroup();
if ( mutationType == MutationType.INSERT
&& mutationTarget instanceof EntityMutationTarget
&& ( (EntityMutationTarget) mutationTarget ).getIdentityInsertDelegate() != null ) {
assert mutationTarget instanceof EntityMappingType;
&& entityMutationOperationGroup != null
&& entityMutationOperationGroup.getMutationTarget().getIdentityInsertDelegate() != null ) {
if ( numberOfOperations > 1 ) {
return new MutationExecutorPostInsert( operationGroup, session );
return new MutationExecutorPostInsert( entityMutationOperationGroup, session );
}
return new MutationExecutorPostInsertSingleTable( operationGroup, session );
return new MutationExecutorPostInsertSingleTable( entityMutationOperationGroup, session );
}
if ( numberOfOperations == 1 ) {

View File

@ -27,6 +27,7 @@ import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.collection.mutation.CollectionMutationTarget;
import org.hibernate.persister.entity.PropertyMapping;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.spi.FromClauseAccess;

View File

@ -20,6 +20,7 @@ import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey;
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.engine.jdbc.mutation.ParameterUsage;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.jdbc.mutation.internal.MutationQueryOptions;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -74,7 +75,6 @@ import org.hibernate.sql.model.ast.MutatingTableReference;
import org.hibernate.sql.model.ast.RestrictedTableMutation;
import org.hibernate.sql.model.ast.TableUpdate;
import org.hibernate.sql.model.ast.builder.TableUpdateBuilderStandard;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.internal.TableUpdateStandard;
import org.hibernate.sql.model.jdbc.JdbcDeleteMutation;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
@ -228,7 +228,7 @@ public class OneToManyPersister extends AbstractCollectionPersister {
final MutationExecutor mutationExecutor = mutationExecutorService.createExecutor(
() -> new BasicBatchKey( getNavigableRole() + "#INDEX" ),
new MutationOperationGroupSingle( MutationType.UPDATE, this, updateRowOperation ),
MutationOperationGroupFactory.singleOperation( MutationType.UPDATE, this, updateRowOperation ),
session
);

View File

@ -12,13 +12,14 @@ import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey;
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER;
@ -34,7 +35,7 @@ public class DeleteRowsCoordinatorStandard implements DeleteRowsCoordinator {
private final BasicBatchKey batchKey;
private final MutationExecutorService mutationExecutorService;
private MutationOperationGroupSingle operationGroup;
private MutationOperationGroup operationGroup;
public DeleteRowsCoordinatorStandard(
CollectionMutationTarget mutationTarget,
@ -113,11 +114,11 @@ public class DeleteRowsCoordinatorStandard implements DeleteRowsCoordinator {
}
}
private MutationOperationGroupSingle createOperationGroup() {
private MutationOperationGroup createOperationGroup() {
assert mutationTarget.getTargetPart() != null;
assert mutationTarget.getTargetPart().getKeyDescriptor() != null;
final JdbcMutationOperation operation = rowMutationOperations.getDeleteRowOperation();
return new MutationOperationGroupSingle( MutationType.DELETE, mutationTarget, operation );
return MutationOperationGroupFactory.singleOperation( MutationType.DELETE, mutationTarget, operation );
}
}

View File

@ -7,12 +7,11 @@
package org.hibernate.persister.collection.mutation;
import java.util.Iterator;
import java.util.function.Supplier;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey;
import org.hibernate.engine.jdbc.batch.spi.BatchKey;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.jdbc.mutation.spi.BatchKeyAccess;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.EntityEntry;
@ -22,8 +21,8 @@ import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.collection.OneToManyPersister;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER;
@ -135,7 +134,7 @@ public class DeleteRowsCoordinatorTablePerSubclass implements DeleteRowsCoordina
);
}
private MutationOperationGroupSingle createOperationGroup(EntityPersister elementPersister) {
private MutationOperationGroup createOperationGroup(EntityPersister elementPersister) {
assert mutationTarget.getTargetPart() != null;
assert mutationTarget.getTargetPart().getKeyDescriptor() != null;
@ -153,16 +152,16 @@ public class DeleteRowsCoordinatorTablePerSubclass implements DeleteRowsCoordina
collectionTableMapping.getDeleteRowDetails()
)
);
return new MutationOperationGroupSingle( MutationType.DELETE, mutationTarget, operation );
return MutationOperationGroupFactory.singleOperation( MutationType.DELETE, mutationTarget, operation );
}
private static class SubclassEntry {
private final BatchKeyAccess batchKeySupplier;
private final MutationOperationGroupSingle operationGroup;
private final MutationOperationGroup operationGroup;
public SubclassEntry(BatchKeyAccess batchKeySupplier, MutationOperationGroupSingle operationGroup) {
public SubclassEntry(BatchKeyAccess batchKeySupplier, MutationOperationGroup operationGroup) {
this.batchKeySupplier = batchKeySupplier;
this.operationGroup = operationGroup;
}

View File

@ -12,13 +12,14 @@ import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey;
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER;
@ -33,7 +34,7 @@ public class InsertRowsCoordinatorStandard implements InsertRowsCoordinator {
private final BasicBatchKey batchKey;
private final MutationExecutorService mutationExecutorService;
private MutationOperationGroupSingle operationGroup;
private MutationOperationGroup operationGroup;
public InsertRowsCoordinatorStandard(
CollectionMutationTarget mutationTarget,
@ -127,11 +128,11 @@ public class InsertRowsCoordinatorStandard implements InsertRowsCoordinator {
}
}
private MutationOperationGroupSingle createOperationGroup() {
private MutationOperationGroup createOperationGroup() {
assert mutationTarget.getTargetPart() != null;
assert mutationTarget.getTargetPart().getKeyDescriptor() != null;
final JdbcMutationOperation operation = rowMutationOperations.getInsertRowOperation();
return new MutationOperationGroupSingle( MutationType.INSERT, mutationTarget, operation );
return MutationOperationGroupFactory.singleOperation( MutationType.INSERT, mutationTarget, operation );
}
}

View File

@ -11,6 +11,7 @@ import java.util.Iterator;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.jdbc.mutation.spi.BatchKeyAccess;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.EntityEntry;
@ -20,8 +21,8 @@ import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.collection.OneToManyPersister;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER;
@ -42,7 +43,10 @@ public class InsertRowsCoordinatorTablePerSubclass implements InsertRowsCoordina
ServiceRegistry serviceRegistry) {
this.mutationTarget = mutationTarget;
this.rowMutationOperations = rowMutationOperations;
this.subclassEntries = new SubclassEntry[mutationTarget.getElementPersister().getRootEntityDescriptor().getSubclassEntityNames().size()];
this.subclassEntries = new SubclassEntry[mutationTarget.getElementPersister()
.getRootEntityDescriptor()
.getSubclassEntityNames()
.size()];
this.mutationExecutorService = serviceRegistry.getService( MutationExecutorService.class );
}
@ -153,7 +157,7 @@ public class InsertRowsCoordinatorTablePerSubclass implements InsertRowsCoordina
);
}
private MutationOperationGroupSingle createOperationGroup(EntityPersister elementPersister) {
private MutationOperationGroup createOperationGroup(EntityPersister elementPersister) {
assert mutationTarget.getTargetPart() != null;
assert mutationTarget.getTargetPart().getKeyDescriptor() != null;
@ -171,16 +175,16 @@ public class InsertRowsCoordinatorTablePerSubclass implements InsertRowsCoordina
collectionTableMapping.getDeleteRowDetails()
)
);
return new MutationOperationGroupSingle( MutationType.INSERT, mutationTarget, operation );
return MutationOperationGroupFactory.singleOperation( MutationType.INSERT, mutationTarget, operation );
}
private static class SubclassEntry {
private final BatchKeyAccess batchKeySupplier;
private final MutationOperationGroupSingle operationGroup;
private final MutationOperationGroup operationGroup;
public SubclassEntry(BatchKeyAccess batchKeySupplier, MutationOperationGroupSingle operationGroup) {
public SubclassEntry(BatchKeyAccess batchKeySupplier, MutationOperationGroup operationGroup) {
this.batchKeySupplier = batchKeySupplier;
this.operationGroup = operationGroup;
}

View File

@ -9,13 +9,14 @@ package org.hibernate.persister.collection.mutation;
import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey;
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.ast.MutatingTableReference;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER;
@ -31,7 +32,7 @@ public class RemoveCoordinatorStandard implements RemoveCoordinator {
private final BasicBatchKey batchKey;
private final MutationExecutorService mutationExecutorService;
private MutationOperationGroupSingle operationGroup;
private MutationOperationGroup operationGroup;
/**
* Creates the coordinator.
@ -67,7 +68,7 @@ public class RemoveCoordinatorStandard implements RemoveCoordinator {
operationGroup = buildOperationGroup();
}
final JdbcMutationOperation operation = operationGroup.getSingleOperation();
final JdbcMutationOperation operation = (JdbcMutationOperation) operationGroup.getSingleOperation();
return operation.getSqlString();
}
@ -117,7 +118,7 @@ public class RemoveCoordinatorStandard implements RemoveCoordinator {
}
}
private MutationOperationGroupSingle buildOperationGroup() {
private MutationOperationGroup buildOperationGroup() {
assert mutationTarget.getTargetPart() != null;
assert mutationTarget.getTargetPart().getKeyDescriptor() != null;
@ -128,7 +129,7 @@ public class RemoveCoordinatorStandard implements RemoveCoordinator {
final CollectionTableMapping tableMapping = mutationTarget.getCollectionTableMapping();
final MutatingTableReference tableReference = new MutatingTableReference( tableMapping );
return new MutationOperationGroupSingle(
return MutationOperationGroupFactory.singleOperation(
MutationType.DELETE,
mutationTarget,
operationProducer.createOperation( tableReference )

View File

@ -9,6 +9,7 @@ package org.hibernate.persister.collection.mutation;
import java.util.Collection;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.EntityMappingType;
@ -16,9 +17,9 @@ import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.persister.collection.OneToManyPersister;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.ast.MutatingTableReference;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER;
@ -30,7 +31,7 @@ public class RemoveCoordinatorTablePerSubclass implements RemoveCoordinator {
private final OperationProducer operationProducer;
private final MutationExecutorService mutationExecutorService;
private MutationOperationGroupSingle[] operationGroups;
private MutationOperationGroup[] operationGroups;
/**
* Creates the coordinator.
@ -72,7 +73,7 @@ public class RemoveCoordinatorTablePerSubclass implements RemoveCoordinator {
);
}
MutationOperationGroupSingle[] operationGroups = this.operationGroups;
MutationOperationGroup[] operationGroups = this.operationGroups;
if ( operationGroups == null ) {
// delayed creation of the operation-group
operationGroups = this.operationGroups = buildOperationGroups();
@ -80,7 +81,7 @@ public class RemoveCoordinatorTablePerSubclass implements RemoveCoordinator {
final ForeignKeyDescriptor fkDescriptor = mutationTarget.getTargetPart().getKeyDescriptor();
for ( MutationOperationGroupSingle operationGroup : operationGroups ) {
for ( MutationOperationGroup operationGroup : operationGroups ) {
final MutationExecutor mutationExecutor = mutationExecutorService.createExecutor(
() -> null,
operationGroup,
@ -111,11 +112,11 @@ public class RemoveCoordinatorTablePerSubclass implements RemoveCoordinator {
}
}
private MutationOperationGroupSingle[] buildOperationGroups() {
private MutationOperationGroup[] buildOperationGroups() {
final Collection<EntityMappingType> subMappingTypes = mutationTarget.getElementPersister()
.getRootEntityDescriptor()
.getSubMappingTypes();
final MutationOperationGroupSingle[] operationGroups = new MutationOperationGroupSingle[subMappingTypes.size()];
final MutationOperationGroup[] operationGroups = new MutationOperationGroup[subMappingTypes.size()];
int i = 0;
for ( EntityMappingType subMappingType : subMappingTypes ) {
operationGroups[i++] = buildOperationGroup( subMappingType.getEntityPersister() );
@ -123,7 +124,7 @@ public class RemoveCoordinatorTablePerSubclass implements RemoveCoordinator {
return operationGroups;
}
private MutationOperationGroupSingle buildOperationGroup(EntityPersister elementPersister) {
private MutationOperationGroup buildOperationGroup(EntityPersister elementPersister) {
assert mutationTarget.getTargetPart() != null;
assert mutationTarget.getTargetPart().getKeyDescriptor() != null;
@ -146,7 +147,7 @@ public class RemoveCoordinatorTablePerSubclass implements RemoveCoordinator {
)
);
return new MutationOperationGroupSingle(
return MutationOperationGroupFactory.singleOperation(
MutationType.DELETE,
mutationTarget,
operationProducer.createOperation( tableReference )

View File

@ -12,12 +12,13 @@ import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey;
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
/**
@ -26,8 +27,8 @@ import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
public class UpdateRowsCoordinatorOneToMany extends AbstractUpdateRowsCoordinator {
private final RowMutationOperations rowMutationOperations;
private MutationOperationGroupSingle deleteOperationGroup;
private MutationOperationGroupSingle insertOperationGroup;
private MutationOperationGroup deleteOperationGroup;
private MutationOperationGroup insertOperationGroup;
public UpdateRowsCoordinatorOneToMany(
CollectionMutationTarget mutationTarget,
@ -58,7 +59,7 @@ public class UpdateRowsCoordinatorOneToMany extends AbstractUpdateRowsCoordinato
return;
}
final MutationOperationGroupSingle operationGroup = resolveDeleteGroup();
final MutationOperationGroup operationGroup = resolveDeleteGroup();
final MutationExecutor mutationExecutor = mutationExecutorService.createExecutor(
() -> new BasicBatchKey( getMutationTarget().getRolePath() + "#UPDATE-DELETE" ),
operationGroup,
@ -96,12 +97,12 @@ public class UpdateRowsCoordinatorOneToMany extends AbstractUpdateRowsCoordinato
}
}
private MutationOperationGroupSingle resolveDeleteGroup() {
private MutationOperationGroup resolveDeleteGroup() {
if ( deleteOperationGroup == null ) {
final JdbcMutationOperation operation = rowMutationOperations.getDeleteRowOperation();
assert operation != null;
deleteOperationGroup = new MutationOperationGroupSingle( MutationType.DELETE, getMutationTarget(), operation );
deleteOperationGroup = MutationOperationGroupFactory.singleOperation( MutationType.DELETE, getMutationTarget(), operation );
}
return deleteOperationGroup;
@ -115,7 +116,7 @@ public class UpdateRowsCoordinatorOneToMany extends AbstractUpdateRowsCoordinato
return -1;
}
final MutationOperationGroupSingle operationGroup = resolveInsertGroup();
final MutationOperationGroup operationGroup = resolveInsertGroup();
final MutationExecutor mutationExecutor = mutationExecutorService.createExecutor(
() -> new BasicBatchKey( getMutationTarget().getRolePath() + "#UPDATE-INSERT" ),
operationGroup,
@ -154,12 +155,12 @@ public class UpdateRowsCoordinatorOneToMany extends AbstractUpdateRowsCoordinato
}
}
private MutationOperationGroupSingle resolveInsertGroup() {
private MutationOperationGroup resolveInsertGroup() {
if ( insertOperationGroup == null ) {
final JdbcMutationOperation operation = rowMutationOperations.getInsertRowOperation();
assert operation != null;
insertOperationGroup = new MutationOperationGroupSingle( MutationType.INSERT, getMutationTarget(), operation );
insertOperationGroup = MutationOperationGroupFactory.singleOperation( MutationType.INSERT, getMutationTarget(), operation );
}
return insertOperationGroup;

View File

@ -13,13 +13,12 @@ import java.util.List;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.internal.AbstractMutationOperationGroup;
import org.hibernate.sql.model.internal.MutationOperationGroupNone;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
/**
@ -32,7 +31,7 @@ import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
public class UpdateRowsCoordinatorStandard extends AbstractUpdateRowsCoordinator implements UpdateRowsCoordinator {
private final RowMutationOperations rowMutationOperations;
private AbstractMutationOperationGroup operationGroup;
private MutationOperationGroup operationGroup;
public UpdateRowsCoordinatorStandard(
CollectionMutationTarget mutationTarget,
@ -44,7 +43,7 @@ public class UpdateRowsCoordinatorStandard extends AbstractUpdateRowsCoordinator
@Override
protected int doUpdate(Object key, PersistentCollection<?> collection, SharedSessionContractImplementor session) {
final AbstractMutationOperationGroup operationGroup = getOperationGroup();
final MutationOperationGroup operationGroup = getOperationGroup();
final MutationExecutor mutationExecutor = mutationExecutorService.createExecutor(
() -> new BasicBatchKey( getMutationTarget().getRolePath() + "#UPDATE" ),
@ -144,21 +143,14 @@ public class UpdateRowsCoordinatorStandard extends AbstractUpdateRowsCoordinator
}
}
protected AbstractMutationOperationGroup getOperationGroup() {
protected MutationOperationGroup getOperationGroup() {
if ( operationGroup == null ) {
final JdbcMutationOperation updateRowOperation = rowMutationOperations.getUpdateRowOperation();
if ( updateRowOperation == null ) {
operationGroup = new MutationOperationGroupNone(
MutationType.UPDATE,
getMutationTarget()
);
operationGroup = MutationOperationGroupFactory.noOperations( MutationType.UPDATE, getMutationTarget() );
}
else {
operationGroup = new MutationOperationGroupSingle(
MutationType.UPDATE,
getMutationTarget(),
updateRowOperation
);
operationGroup = MutationOperationGroupFactory.singleOperation( MutationType.UPDATE, getMutationTarget(), updateRowOperation );
}
}
return operationGroup;

View File

@ -11,6 +11,7 @@ import java.util.Iterator;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.jdbc.mutation.spi.BatchKeyAccess;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -19,8 +20,8 @@ import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.collection.OneToManyPersister;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
/**
@ -124,7 +125,7 @@ public class UpdateRowsCoordinatorTablePerSubclass extends AbstractUpdateRowsCoo
);
}
private MutationOperationGroupSingle resolveDeleteGroup(EntityPersister elementPersister) {
private MutationOperationGroup resolveDeleteGroup(EntityPersister elementPersister) {
final CollectionTableMapping collectionTableMapping = getMutationTarget().getCollectionTableMapping();
final JdbcMutationOperation operation = rowMutationOperations.getDeleteRowOperation(
new CollectionTableMapping(
@ -140,7 +141,7 @@ public class UpdateRowsCoordinatorTablePerSubclass extends AbstractUpdateRowsCoo
)
);
return new MutationOperationGroupSingle( MutationType.DELETE, getMutationTarget(), operation );
return MutationOperationGroupFactory.singleOperation( MutationType.DELETE, getMutationTarget(), operation );
}
private int insertRows(Object key, PersistentCollection<?> collection, SharedSessionContractImplementor session) {
@ -213,7 +214,7 @@ public class UpdateRowsCoordinatorTablePerSubclass extends AbstractUpdateRowsCoo
);
}
private MutationOperationGroupSingle resolveInsertGroup(EntityPersister elementPersister) {
private MutationOperationGroup resolveInsertGroup(EntityPersister elementPersister) {
final CollectionTableMapping collectionTableMapping = getMutationTarget().getCollectionTableMapping();
final JdbcMutationOperation operation = rowMutationOperations.getInsertRowOperation(
new CollectionTableMapping(
@ -229,16 +230,16 @@ public class UpdateRowsCoordinatorTablePerSubclass extends AbstractUpdateRowsCoo
)
);
return new MutationOperationGroupSingle( MutationType.INSERT, getMutationTarget(), operation );
return MutationOperationGroupFactory.singleOperation( MutationType.INSERT, getMutationTarget(), operation );
}
private static class SubclassEntry {
private final BatchKeyAccess batchKeySupplier;
private final MutationOperationGroupSingle operationGroup;
private final MutationOperationGroup operationGroup;
public SubclassEntry(BatchKeyAccess batchKeySupplier, MutationOperationGroupSingle operationGroup) {
public SubclassEntry(BatchKeyAccess batchKeySupplier, MutationOperationGroup operationGroup) {
this.batchKeySupplier = batchKeySupplier;
this.operationGroup = operationGroup;
}

View File

@ -6,8 +6,6 @@
*/
package org.hibernate.persister.entity.mutation;
import java.util.List;
import org.hibernate.Internal;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.batch.spi.BatchKey;
@ -27,14 +25,10 @@ import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.ValuesAnalysis;
import org.hibernate.sql.model.ast.MutationGroup;
import org.hibernate.sql.model.ast.TableMutation;
import org.hibernate.sql.model.ast.builder.ColumnValuesTableMutationBuilder;
import org.hibernate.sql.model.ast.builder.MutationGroupBuilder;
import org.hibernate.sql.model.internal.MutationOperationGroupNone;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.internal.MutationOperationGroupStandard;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
import static org.hibernate.internal.util.collections.CollectionHelper.arrayList;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
/**
* Base support for coordinating mutations against an entity
@ -86,29 +80,38 @@ public abstract class AbstractMutationCoordinator {
final int numberOfTableMutations = mutationGroup.getNumberOfTableMutations();
switch ( numberOfTableMutations ) {
case 0:
return new MutationOperationGroupNone( mutationGroup );
return MutationOperationGroupFactory.noOperations( mutationGroup );
case 1: {
final MutationOperation operation = mutationGroup.getSingleTableMutation()
.createMutationOperation( valuesAnalysis, factory() );
return operation == null
? new MutationOperationGroupNone( mutationGroup )
: new MutationOperationGroupSingle( mutationGroup, (JdbcMutationOperation) operation );
? MutationOperationGroupFactory.noOperations( mutationGroup )
: MutationOperationGroupFactory.singleOperation( mutationGroup, operation );
}
default: {
final List<MutationOperation> operations = arrayList( numberOfTableMutations );
mutationGroup.forEachTableMutation( (integer, tableMutation) -> {
MutationOperation[] operations = new MutationOperation[numberOfTableMutations];
int outputIndex = 0;
int skipped = 0;
for ( int i = 0; i < mutationGroup.getNumberOfTableMutations(); i++ ) {
final TableMutation tableMutation = mutationGroup.getTableMutation( i );
final MutationOperation operation = tableMutation.createMutationOperation( valuesAnalysis, factory );
if ( operation != null ) {
operations.add( operation );
operations[outputIndex++] = operation;
}
else {
skipped++;
ModelMutationLogging.MODEL_MUTATION_LOGGER.debugf(
"Skipping table update - %s",
tableMutation.getTableName()
);
}
} );
return new MutationOperationGroupStandard( mutationGroup.getMutationType(), entityPersister, operations );
}
if ( skipped != 0 ) {
final MutationOperation[] trimmed = new MutationOperation[outputIndex];
System.arraycopy( operations, 0, trimmed, 0, outputIndex );
operations = trimmed;
}
return MutationOperationGroupFactory.manyOperations( mutationGroup.getMutationType(), entityPersister, operations );
}
}
}

View File

@ -114,7 +114,8 @@ public class InsertCoordinator extends AbstractMutationCoordinator {
}
protected void preInsertInMemoryValueGeneration(Object[] values, Object entity, SharedSessionContractImplementor session) {
final EntityMetamodel entityMetamodel = entityPersister().getEntityMetamodel();
final AbstractEntityPersister persister = entityPersister();
final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
if ( entityMetamodel.hasPreInsertGeneratedValues() ) {
final Generator[] generators = entityMetamodel.getGenerators();
for ( int i = 0; i < generators.length; i++ ) {
@ -123,7 +124,7 @@ public class InsertCoordinator extends AbstractMutationCoordinator {
&& !generator.generatedOnExecution()
&& generator.generatesOnInsert() ) {
values[i] = ( (BeforeExecutionGenerator) generator ).generate( session, entity, values[i], INSERT );
entityPersister().setPropertyValue( entity, i, values[i] );
persister.setPropertyValue( entity, i, values[i] );
}
}
}
@ -214,12 +215,12 @@ public class InsertCoordinator extends AbstractMutationCoordinator {
}
}
for ( int position = 0; position < mutationGroup.getNumberOfOperations(); position++ ) {
final MutationOperation jdbcOperation = mutationGroup.getOperation( position );
if ( id == null ) {
assert entityPersister().getIdentityInsertDelegate() != null;
}
else {
if ( id == null ) {
assert entityPersister().getIdentityInsertDelegate() != null;
}
else {
for ( int position = 0; position < mutationGroup.getNumberOfOperations(); position++ ) {
final MutationOperation jdbcOperation = mutationGroup.getOperation( position );
final EntityTableMapping tableDetails = (EntityTableMapping) jdbcOperation.getTableDetails();
breakDownJdbcValue( id, session, jdbcValueBindings, tableDetails );
}

View File

@ -6,11 +6,11 @@
*/
package org.hibernate.persister.entity.mutation;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.internal.MutationOperationGroupNone;
/**
* @author Steve Ebersole
@ -19,7 +19,7 @@ public class UpdateCoordinatorNoOp implements UpdateCoordinator {
private final MutationOperationGroup operationGroup;
public UpdateCoordinatorNoOp(AbstractEntityPersister entityPersister) {
operationGroup = new MutationOperationGroupNone( MutationType.UPDATE, entityPersister );
operationGroup = MutationOperationGroupFactory.noOperations( MutationType.UPDATE, entityPersister );
}
@Override

View File

@ -22,6 +22,7 @@ import org.hibernate.engine.jdbc.batch.spi.BatchKey;
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings;
import org.hibernate.engine.jdbc.mutation.MutationExecutor;
import org.hibernate.engine.jdbc.mutation.ParameterUsage;
import org.hibernate.sql.model.internal.MutationOperationGroupFactory;
import org.hibernate.engine.jdbc.mutation.internal.MutationQueryOptions;
import org.hibernate.engine.jdbc.mutation.internal.NoBatchKeyAccess;
import org.hibernate.engine.jdbc.mutation.spi.BatchKeyAccess;
@ -51,7 +52,6 @@ import org.hibernate.sql.model.ast.builder.RestrictedTableMutationBuilder;
import org.hibernate.sql.model.ast.builder.TableUpdateBuilder;
import org.hibernate.sql.model.ast.builder.TableUpdateBuilderSkipped;
import org.hibernate.sql.model.ast.builder.TableUpdateBuilderStandard;
import org.hibernate.sql.model.internal.MutationOperationGroupSingle;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
import org.hibernate.tuple.entity.EntityMetamodel;
@ -1682,7 +1682,7 @@ public class UpdateCoordinatorStandard extends AbstractMutationCoordinator imple
.buildModelMutationTranslator( updateBuilder.buildMutation(), factory() )
.translate( null, MutationQueryOptions.INSTANCE );
return new MutationOperationGroupSingle( MutationType.UPDATE, entityPersister(), jdbcMutation );
return MutationOperationGroupFactory.singleOperation( MutationType.UPDATE, entityPersister(), jdbcMutation );
}
}

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.sql.model;
import org.hibernate.engine.jdbc.mutation.internal.EntityMutationOperationGroup;
/**
* Group of {@link MutationOperation} references for a specific
* logical operation (target + type)
@ -22,7 +24,7 @@ public interface MutationOperationGroup {
/**
* The model-part being mutated
*/
MutationTarget<?> getMutationTarget();
MutationTarget getMutationTarget();
/**
* Number of operations in this group
@ -48,4 +50,12 @@ public interface MutationOperationGroup {
*/
MutationOperation getOperation(String tableName);
/**
* Attempt to cast to the frequently uses subtype EntityMutationOperationGroup;
* returns null if this is not possible.
* @return
*/
default EntityMutationOperationGroup asEntityMutationOperationGroup() {
return null;
}
}

View File

@ -8,6 +8,7 @@ package org.hibernate.sql.model.ast;
import java.util.function.BiConsumer;
import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationTarget;
import org.hibernate.sql.model.MutationType;
@ -26,7 +27,11 @@ public interface MutationGroup {
TableMutation getSingleTableMutation();
TableMutation getTableMutation(String tableName);
@Deprecated(forRemoval = true)
<O extends MutationOperation, M extends TableMutation<O>> M getTableMutation(String tableName);
void forEachTableMutation(BiConsumer<Integer, TableMutation> action);
@Deprecated(forRemoval = true)
<O extends MutationOperation, M extends TableMutation<O>> void forEachTableMutation(BiConsumer<Integer, M> action);
TableMutation getTableMutation(int i);
}

View File

@ -1,34 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.sql.model.internal;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationTarget;
import org.hibernate.sql.model.MutationType;
/**
* @author Steve Ebersole
*/
public abstract class AbstractMutationOperationGroup implements MutationOperationGroup {
private final MutationType mutationType;
private final MutationTarget<?> mutationTarget;
public AbstractMutationOperationGroup(MutationType mutationType, MutationTarget<?> mutationTarget) {
this.mutationType = mutationType;
this.mutationTarget = mutationTarget;
}
@Override
public MutationType getMutationType() {
return mutationType;
}
@Override
public MutationTarget<?> getMutationTarget() {
return mutationTarget;
}
}

View File

@ -0,0 +1,106 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.sql.model.internal;
import java.util.Locale;
import org.hibernate.engine.jdbc.mutation.internal.EntityMutationOperationGroup;
import org.hibernate.persister.entity.mutation.EntityMutationTarget;
import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationType;
public class EntityMutationOperationGroupStandard implements EntityMutationOperationGroup {
private static final MutationOperation[] EMPTY = new MutationOperation[0];
private final MutationType mutationType;
private final EntityMutationTarget mutationTarget;
private final MutationOperation[] operations;
/**
* Intentionally package private: use {@link MutationOperationGroupFactory}.
* Constructor for when there are no operations.
* @param mutationType
* @param mutationTarget
*/
EntityMutationOperationGroupStandard(MutationType mutationType, EntityMutationTarget mutationTarget) {
this( mutationType, mutationTarget, EMPTY );
}
/**
* Intentionally package private: use {@link MutationOperationGroupFactory}.
* Constructor for when there's a single operation.
* @param mutationType
* @param mutationTarget
* @param operation
*/
EntityMutationOperationGroupStandard(MutationType mutationType, EntityMutationTarget mutationTarget, MutationOperation operation) {
this( mutationType, mutationTarget, new MutationOperation[]{ operation } );
}
/**
* Intentionally package private: use {@link MutationOperationGroupFactory}.
* Constructor for when there's multiple operations.
* @param mutationType
* @param mutationTarget
* @param operations
*/
EntityMutationOperationGroupStandard(MutationType mutationType, EntityMutationTarget mutationTarget, MutationOperation[] operations) {
this.mutationType = mutationType;
this.mutationTarget = mutationTarget;
this.operations = operations;
}
@Override
public MutationType getMutationType() {
return mutationType;
}
@Override
public EntityMutationTarget getMutationTarget() {
return mutationTarget;
}
@Override
public int getNumberOfOperations() {
return operations.length;
}
@Override
public MutationOperation getSingleOperation() {
if ( operations.length == 1 ) {
return operations[0];
}
else {
throw new IllegalStateException(
String.format(
Locale.ROOT,
"Group contains multiple table mutations - %s : %s ",
getMutationType().name(),
getMutationTarget().getNavigableRole()
)
);
}
}
@Override
public MutationOperation getOperation(int idx) {
return operations[idx];
}
@Override
public MutationOperation getOperation(final String tableName) {
for ( int i = 0; i < operations.length; i++ ) {
final MutationOperation operation = operations[i];
if ( operation.getTableDetails().getTableName().equals( tableName ) ) {
return operation;
}
}
return null;
}
}

View File

@ -9,6 +9,7 @@ package org.hibernate.sql.model.internal;
import java.util.Locale;
import java.util.function.BiConsumer;
import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationTarget;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.ast.MutationGroup;
@ -55,7 +56,12 @@ public class MutationGroupNone implements MutationGroup {
}
@Override
public void forEachTableMutation(BiConsumer<Integer, TableMutation> action) {
public <O extends MutationOperation, M extends TableMutation<O>> void forEachTableMutation(BiConsumer<Integer, M> action) {
}
@Override
public TableMutation getTableMutation(int i) {
return null;
}
@Override

View File

@ -9,6 +9,7 @@ package org.hibernate.sql.model.internal;
import java.util.Locale;
import java.util.function.BiConsumer;
import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationTarget;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.ast.MutationGroup;
@ -61,8 +62,14 @@ public class MutationGroupSingle implements MutationGroup {
}
@Override
public void forEachTableMutation(BiConsumer<Integer, TableMutation> action) {
action.accept( 0, tableMutation );
public <O extends MutationOperation, M extends TableMutation<O>> void forEachTableMutation(BiConsumer<Integer, M> action) {
//noinspection unchecked
action.accept( 0, (M) tableMutation );
}
@Override
public TableMutation getTableMutation(int i) {
return tableMutation;
}
@Override

View File

@ -9,6 +9,7 @@ package org.hibernate.sql.model.internal;
import java.util.List;
import java.util.function.BiConsumer;
import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationTarget;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.ast.MutationGroup;
@ -67,10 +68,16 @@ public class MutationGroupStandard implements MutationGroup {
}
@Override
public void forEachTableMutation(BiConsumer<Integer, TableMutation> action) {
public <O extends MutationOperation, M extends TableMutation<O>> void forEachTableMutation(BiConsumer<Integer, M> action) {
for ( int i = 0; i < tableMutationList.size(); i++ ) {
action.accept( i, tableMutationList.get( i ) );
//noinspection unchecked
action.accept( i, (M)tableMutationList.get( i ) );
}
}
@Override
public TableMutation getTableMutation(int i) {
return tableMutationList.get( i );
}
}

View File

@ -0,0 +1,74 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.sql.model.internal;
import org.hibernate.persister.entity.mutation.EntityMutationTarget;
import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationTarget;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.ast.MutationGroup;
public final class MutationOperationGroupFactory {
public static MutationOperationGroup noOperations(
final MutationType mutationType,
final MutationTarget mutationTarget) {
if ( mutationTarget instanceof EntityMutationTarget ) {
return new EntityMutationOperationGroupStandard(
mutationType,
(EntityMutationTarget) mutationTarget
);
}
else {
return new MutationOperationGroupStandard(
mutationType,
mutationTarget
);
}
}
public static MutationOperationGroup noOperations(final MutationGroup mutationGroup) {
return noOperations( mutationGroup.getMutationType(), mutationGroup.getMutationTarget() );
}
public static MutationOperationGroup singleOperation(
final MutationType mutationType,
final MutationTarget mutationTarget,
final MutationOperation operation) {
if ( mutationTarget instanceof EntityMutationTarget ) {
return new EntityMutationOperationGroupStandard(
mutationType,
(EntityMutationTarget) mutationTarget,
operation
);
}
else {
return new MutationOperationGroupStandard(
mutationType,
mutationTarget,
operation
);
}
}
public static MutationOperationGroup singleOperation(final MutationGroup mutationGroup, final MutationOperation operation) {
return singleOperation( mutationGroup.getMutationType(), mutationGroup.getMutationTarget(), operation );
}
public static MutationOperationGroup manyOperations(
final MutationType mutationType,
final MutationTarget mutationTarget,
final MutationOperation[] operations) {
if ( mutationTarget instanceof EntityMutationTarget ) {
return new EntityMutationOperationGroupStandard( mutationType, (EntityMutationTarget) mutationTarget, operations );
}
else {
return new MutationOperationGroupStandard( mutationType, mutationTarget, operations );
}
}
}

View File

@ -1,47 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.sql.model.internal;
import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationTarget;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.ast.MutationGroup;
/**
* Specialized MutationOperationGroup for case of no operations
*
* @author Steve Ebersole
*/
public class MutationOperationGroupNone extends AbstractMutationOperationGroup {
public MutationOperationGroupNone(MutationType mutationType, MutationTarget<?> mutationTarget) {
super( mutationType, mutationTarget );
}
public MutationOperationGroupNone(MutationGroup mutationGroup) {
this( mutationGroup.getMutationType(), mutationGroup.getMutationTarget() );
}
@Override
public int getNumberOfOperations() {
return 0;
}
@Override
public MutationOperation getSingleOperation() {
return null;
}
@Override
public MutationOperation getOperation(int idx) {
throw new IndexOutOfBoundsException( idx );
}
@Override
public MutationOperation getOperation(String tableName) {
return null;
}
}

View File

@ -1,67 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.sql.model.internal;
import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationTarget;
import org.hibernate.sql.model.MutationType;
import org.hibernate.sql.model.ast.MutationGroup;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER;
/**
* @author Steve Ebersole
*/
public class MutationOperationGroupSingle extends AbstractMutationOperationGroup {
private final JdbcMutationOperation operation;
public MutationOperationGroupSingle(
MutationType mutationType,
MutationTarget<?> mutationTarget,
JdbcMutationOperation operation) {
super( mutationType, mutationTarget );
this.operation = operation;
}
public MutationOperationGroupSingle(MutationGroup mutationGroup, JdbcMutationOperation operation) {
this( mutationGroup.getMutationType(), mutationGroup.getMutationTarget(), operation );
}
@Override
public int getNumberOfOperations() {
return 1;
}
@Override
public JdbcMutationOperation getSingleOperation() {
return operation;
}
@Override
public MutationOperation getOperation(int idx) {
if ( idx != 0 ) {
throw new IndexOutOfBoundsException( idx );
}
return operation;
}
@Override
public MutationOperation getOperation(String tableName) {
if ( !tableName.equals( operation.getTableDetails().getTableName() ) ) {
MODEL_MUTATION_LOGGER.debugf(
"Unexpected table name mismatch : `%s` - `%s`",
tableName,
operation.getTableDetails().getTableName()
);
}
return operation;
}
}

View File

@ -6,53 +6,100 @@
*/
package org.hibernate.sql.model.internal;
import java.util.List;
import java.util.Locale;
import org.hibernate.sql.model.MutationOperation;
import org.hibernate.sql.model.MutationOperationGroup;
import org.hibernate.sql.model.MutationTarget;
import org.hibernate.sql.model.MutationType;
/**
* @author Steve Ebersole
* @author Sanne Grinovero
*/
public class MutationOperationGroupStandard extends AbstractMutationOperationGroup {
private final List<MutationOperation> operations;
final class MutationOperationGroupStandard implements MutationOperationGroup {
public MutationOperationGroupStandard(MutationType mutationType, MutationTarget<?> mutationTarget, List<MutationOperation> operations) {
super( mutationType, mutationTarget );
private static final MutationOperation[] EMPTY = new MutationOperation[0];
private final MutationType mutationType;
private final MutationTarget mutationTarget;
private final MutationOperation[] operations;
/**
* Intentionally package private: use {@link MutationOperationGroupFactory}.
* Constructor for when there are no operations.
* @param mutationType
* @param mutationTarget
*/
MutationOperationGroupStandard(MutationType mutationType, MutationTarget mutationTarget) {
this( mutationType, mutationTarget, EMPTY );
}
/**
* Intentionally package private: use {@link MutationOperationGroupFactory}.
* Constructor for when there's a single operation.
* @param mutationType
* @param mutationTarget
* @param operation
*/
MutationOperationGroupStandard(MutationType mutationType, MutationTarget mutationTarget, MutationOperation operation) {
this( mutationType, mutationTarget, new MutationOperation[]{ operation } );
}
/**
* Intentionally package private: use {@link MutationOperationGroupFactory}.
* Constructor for when there's multiple operations.
* @param mutationType
* @param mutationTarget
* @param operations
*/
MutationOperationGroupStandard(MutationType mutationType, MutationTarget mutationTarget, MutationOperation[] operations) {
this.mutationType = mutationType;
this.mutationTarget = mutationTarget;
this.operations = operations;
}
@Override
public MutationType getMutationType() {
return mutationType;
}
@Override
public MutationTarget getMutationTarget() {
return mutationTarget;
}
@Override
public int getNumberOfOperations() {
return operations.size();
return operations.length;
}
@Override
public MutationOperation getSingleOperation() {
if ( operations.size() == 1 ) {
return operations.get( 0 );
if ( operations.length == 1 ) {
return operations[0];
}
else {
throw new IllegalStateException(
String.format(
Locale.ROOT,
"Group contains multiple table mutations - %s : %s ",
getMutationType().name(),
getMutationTarget().getNavigableRole()
)
);
}
throw new IllegalStateException(
String.format(
Locale.ROOT,
"Group contains multiple table mutations - %s : %s ",
getMutationType().name(),
getMutationTarget().getNavigableRole()
)
);
}
@Override
public MutationOperation getOperation(int idx) {
return operations.get( idx );
return operations[idx];
}
@Override
public MutationOperation getOperation(String tableName) {
for ( int i = 0; i < operations.size(); i++ ) {
final MutationOperation operation = operations.get( i );
public MutationOperation getOperation(final String tableName) {
for ( int i = 0; i < operations.length; i++ ) {
final MutationOperation operation = operations[i];
if ( operation.getTableDetails().getTableName().equals( tableName ) ) {
return operation;
}