Search in sources :

Example 1 with SqmJdbcExecutionContextAdapter

use of org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter in project hibernate-orm by hibernate.

the class InlineDeleteHandler method executeDelete.

private void executeDelete(String targetTableExpression, EntityMappingType entityDescriptor, Supplier<Consumer<SelectableConsumer>> tableKeyColumnsVisitationSupplier, List<Object> ids, int valueIndex, ModelPart valueModelPart, JdbcParameterBindings jdbcParameterBindings, DomainQueryExecutionContext executionContext) {
    final NamedTableReference targetTableReference = new NamedTableReference(targetTableExpression, DeleteStatement.DEFAULT_ALIAS, false, sessionFactory);
    final SqmJdbcExecutionContextAdapter executionContextAdapter = SqmJdbcExecutionContextAdapter.omittingLockingAndPaging(executionContext);
    final Predicate matchingIdsPredicate = matchingIdsPredicateProducer.produceRestriction(ids, entityDescriptor, valueIndex, valueModelPart, targetTableReference, tableKeyColumnsVisitationSupplier, executionContextAdapter);
    final DeleteStatement deleteStatement = new DeleteStatement(targetTableReference, matchingIdsPredicate);
    final JdbcDelete jdbcOperation = sqlAstTranslatorFactory.buildDeleteTranslator(sessionFactory, deleteStatement).translate(jdbcParameterBindings, executionContext.getQueryOptions());
    jdbcMutationExecutor.execute(jdbcOperation, jdbcParameterBindings, this::prepareQueryStatement, (integer, preparedStatement) -> {
    }, executionContextAdapter);
}
Also used : NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) JdbcDelete(org.hibernate.sql.exec.spi.JdbcDelete) SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) DeleteStatement(org.hibernate.sql.ast.tree.delete.DeleteStatement) SqmJdbcExecutionContextAdapter(org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 2 with SqmJdbcExecutionContextAdapter

use of org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter in project hibernate-orm by hibernate.

the class RestrictedDeleteExecutionDelegate method execute.

@Override
public int execute(DomainQueryExecutionContext executionContext) {
    final EntityPersister entityDescriptor = sessionFactory.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(sqmDelete.getTarget().getEntityName());
    final String hierarchyRootTableName = ((Joinable) entityDescriptor).getTableName();
    final TableGroup deletingTableGroup = converter.getMutatingTableGroup();
    final TableReference hierarchyRootTableReference = deletingTableGroup.resolveTableReference(deletingTableGroup.getNavigablePath(), hierarchyRootTableName);
    assert hierarchyRootTableReference != null;
    final Map<SqmParameter<?>, List<List<JdbcParameter>>> parameterResolutions;
    final Map<SqmParameter<?>, MappingModelExpressible<?>> paramTypeResolutions;
    if (domainParameterXref.getSqmParameterCount() == 0) {
        parameterResolutions = Collections.emptyMap();
        paramTypeResolutions = Collections.emptyMap();
    } else {
        parameterResolutions = new IdentityHashMap<>();
        paramTypeResolutions = new LinkedHashMap<>();
    }
    // Use the converter to interpret the where-clause.  We do this for 2 reasons:
    // 1) the resolved Predicate is ultimately the base for applying restriction to the deletes
    // 2) we also inspect each ColumnReference that is part of the where-clause to see which
    // table it comes from.  if all of the referenced columns (if any at all) are from the root table
    // we can perform all of the deletes without using an id-table
    final MutableBoolean needsIdTableWrapper = new MutableBoolean(false);
    final Predicate specifiedRestriction = converter.visitWhereClause(sqmDelete.getWhereClause(), columnReference -> {
        if (!hierarchyRootTableReference.getIdentificationVariable().equals(columnReference.getQualifier())) {
            needsIdTableWrapper.setValue(true);
        }
    }, (sqmParameter, mappingType, jdbcParameters) -> {
        parameterResolutions.computeIfAbsent(sqmParameter, k -> new ArrayList<>(1)).add(jdbcParameters);
        paramTypeResolutions.put(sqmParameter, mappingType);
    });
    final PredicateCollector predicateCollector = new PredicateCollector(specifiedRestriction);
    entityDescriptor.applyBaseRestrictions((filterPredicate) -> {
        needsIdTableWrapper.setValue(true);
        predicateCollector.applyPredicate(filterPredicate);
    }, deletingTableGroup, true, executionContext.getSession().getLoadQueryInfluencers().getEnabledFilters(), null, converter);
    converter.pruneTableGroupJoins();
    // We need an id table if we want to delete from an intermediate table to avoid FK violations
    // The intermediate table has a FK to the root table, so we can't delete from the root table first
    // Deleting from the intermediate table first also isn't possible,
    // because that is the source for deletion in other tables, hence we need an id table
    final boolean needsIdTable = needsIdTableWrapper.getValue() || entityDescriptor != entityDescriptor.getRootEntityDescriptor();
    final SqmJdbcExecutionContextAdapter executionContextAdapter = SqmJdbcExecutionContextAdapter.omittingLockingAndPaging(executionContext);
    if (needsIdTable) {
        return executeWithIdTable(predicateCollector.getPredicate(), deletingTableGroup, parameterResolutions, paramTypeResolutions, executionContextAdapter);
    } else {
        return executeWithoutIdTable(predicateCollector.getPredicate(), deletingTableGroup, parameterResolutions, paramTypeResolutions, converter.getSqlExpressionResolver(), executionContextAdapter);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) EntityPersister(org.hibernate.persister.entity.EntityPersister) PredicateCollector(org.hibernate.sql.ast.tree.predicate.PredicateCollector) Joinable(org.hibernate.persister.entity.Joinable) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) MappingModelHelper(org.hibernate.metamodel.mapping.MappingModelHelper) SqmJdbcExecutionContextAdapter(org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) SqmUtil(org.hibernate.query.sqm.internal.SqmUtil) Map(java.util.Map) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MutableBoolean(org.hibernate.internal.util.MutableBoolean) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) IdentityHashMap(java.util.IdentityHashMap) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) List(java.util.List) SqmMutationStrategyHelper(org.hibernate.query.sqm.mutation.internal.SqmMutationStrategyHelper) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) QueryOptions(org.hibernate.query.spi.QueryOptions) Logger(org.jboss.logging.Logger) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings) ArrayList(java.util.ArrayList) TableReference(org.hibernate.sql.ast.tree.from.TableReference) LinkedHashMap(java.util.LinkedHashMap) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) MultiTableSqmMutationConverter(org.hibernate.query.sqm.mutation.internal.MultiTableSqmMutationConverter) SqmParameterMappingModelResolutionAccess(org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess) UnionTableReference(org.hibernate.sql.ast.tree.from.UnionTableReference) TemporaryTable(org.hibernate.dialect.temptable.TemporaryTable) DeleteStatement(org.hibernate.sql.ast.tree.delete.DeleteStatement) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) MutableInteger(org.hibernate.internal.util.MutableInteger) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcDelete(org.hibernate.sql.exec.spi.JdbcDelete) DomainQueryExecutionContext(org.hibernate.query.spi.DomainQueryExecutionContext) Consumer(java.util.function.Consumer) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) DomainParameterXref(org.hibernate.query.sqm.internal.DomainParameterXref) Collections(java.util.Collections) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) MutableBoolean(org.hibernate.internal.util.MutableBoolean) ArrayList(java.util.ArrayList) SqmJdbcExecutionContextAdapter(org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) TableReference(org.hibernate.sql.ast.tree.from.TableReference) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) UnionTableReference(org.hibernate.sql.ast.tree.from.UnionTableReference) PredicateCollector(org.hibernate.sql.ast.tree.predicate.PredicateCollector) Joinable(org.hibernate.persister.entity.Joinable) List(java.util.List) ArrayList(java.util.ArrayList) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter)

Aggregations

SqmJdbcExecutionContextAdapter (org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter)2 SqmDeleteStatement (org.hibernate.query.sqm.tree.delete.SqmDeleteStatement)2 DeleteStatement (org.hibernate.sql.ast.tree.delete.DeleteStatement)2 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)2 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)2 JdbcDelete (org.hibernate.sql.exec.spi.JdbcDelete)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 TemporaryTable (org.hibernate.dialect.temptable.TemporaryTable)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)1