Search in sources :

Example 1 with MutableInteger

use of org.hibernate.internal.util.MutableInteger in project hibernate-orm by hibernate.

the class RestrictedDeleteExecutionDelegate method executeWithoutIdTable.

private int executeWithoutIdTable(Predicate suppliedPredicate, TableGroup tableGroup, Map<SqmParameter<?>, List<List<JdbcParameter>>> restrictionSqmParameterResolutions, Map<SqmParameter<?>, MappingModelExpressible<?>> paramTypeResolutions, SqlExpressionResolver sqlExpressionResolver, ExecutionContext executionContext) {
    assert entityDescriptor == entityDescriptor.getRootEntityDescriptor();
    final EntityPersister rootEntityPersister = entityDescriptor.getEntityPersister();
    final String rootTableName = ((Joinable) rootEntityPersister).getTableName();
    final NamedTableReference rootTableReference = (NamedTableReference) tableGroup.resolveTableReference(tableGroup.getNavigablePath(), rootTableName);
    final QuerySpec matchingIdSubQuerySpec = ExecuteWithoutIdTableHelper.createIdMatchingSubQuerySpec(tableGroup.getNavigablePath(), rootTableReference, suppliedPredicate, rootEntityPersister, sqlExpressionResolver, sessionFactory);
    final JdbcParameterBindings jdbcParameterBindings = SqmUtil.createJdbcParameterBindings(executionContext.getQueryParameterBindings(), domainParameterXref, SqmUtil.generateJdbcParamsXref(domainParameterXref, () -> restrictionSqmParameterResolutions), sessionFactory.getRuntimeMetamodels().getMappingMetamodel(), navigablePath -> tableGroup, new SqmParameterMappingModelResolutionAccess() {

        @Override
        @SuppressWarnings("unchecked")
        public <T> MappingModelExpressible<T> getResolvedMappingModelType(SqmParameter<T> parameter) {
            return (MappingModelExpressible<T>) paramTypeResolutions.get(parameter);
        }
    }, executionContext.getSession());
    SqmMutationStrategyHelper.cleanUpCollectionTables(entityDescriptor, (tableReference, attributeMapping) -> {
        // No need for a predicate if there is no supplied predicate i.e. this is a full cleanup
        if (suppliedPredicate == null) {
            return null;
        }
        final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
        final QuerySpec idSelectFkSubQuery;
        // todo (6.0): based on the location of the attribute mapping, we could prune the table group of the subquery
        if (fkDescriptor.getTargetPart() instanceof EntityIdentifierMapping) {
            idSelectFkSubQuery = matchingIdSubQuerySpec;
        } else {
            idSelectFkSubQuery = ExecuteWithoutIdTableHelper.createIdMatchingSubQuerySpec(tableGroup.getNavigablePath(), rootTableReference, suppliedPredicate, rootEntityPersister, sqlExpressionResolver, sessionFactory);
        }
        return new InSubQueryPredicate(MappingModelHelper.buildColumnReferenceExpression(fkDescriptor, null, sessionFactory), idSelectFkSubQuery, false);
    }, jdbcParameterBindings, executionContext);
    if (rootTableReference instanceof UnionTableReference) {
        final MutableInteger rows = new MutableInteger();
        entityDescriptor.visitConstraintOrderedTables((tableExpression, tableKeyColumnVisitationSupplier) -> {
            final NamedTableReference tableReference = new NamedTableReference(tableExpression, tableGroup.getPrimaryTableReference().getIdentificationVariable(), false, sessionFactory);
            final QuerySpec idMatchingSubQuerySpec;
            // No need for a predicate if there is no supplied predicate i.e. this is a full cleanup
            if (suppliedPredicate == null) {
                idMatchingSubQuerySpec = null;
            } else {
                idMatchingSubQuerySpec = matchingIdSubQuerySpec;
            }
            rows.plus(deleteFromNonRootTableWithoutIdTable(tableReference, tableKeyColumnVisitationSupplier, sqlExpressionResolver, tableGroup, idMatchingSubQuerySpec, jdbcParameterBindings, executionContext));
        });
        return rows.get();
    } else {
        entityDescriptor.visitConstraintOrderedTables((tableExpression, tableKeyColumnVisitationSupplier) -> {
            if (!tableExpression.equals(rootTableName)) {
                final NamedTableReference tableReference = (NamedTableReference) tableGroup.getTableReference(tableGroup.getNavigablePath(), tableExpression, true, true);
                final QuerySpec idMatchingSubQuerySpec;
                // No need for a predicate if there is no supplied predicate i.e. this is a full cleanup
                if (suppliedPredicate == null) {
                    idMatchingSubQuerySpec = null;
                } else {
                    idMatchingSubQuerySpec = matchingIdSubQuerySpec;
                }
                deleteFromNonRootTableWithoutIdTable(tableReference, tableKeyColumnVisitationSupplier, sqlExpressionResolver, tableGroup, idMatchingSubQuerySpec, jdbcParameterBindings, executionContext);
            }
        });
        return deleteFromRootTableWithoutIdTable(rootTableReference, suppliedPredicate, jdbcParameterBindings, executionContext);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) SqmParameterMappingModelResolutionAccess(org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) MutableInteger(org.hibernate.internal.util.MutableInteger) UnionTableReference(org.hibernate.sql.ast.tree.from.UnionTableReference) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Joinable(org.hibernate.persister.entity.Joinable) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 2 with MutableInteger

use of org.hibernate.internal.util.MutableInteger in project hibernate-orm by hibernate.

the class CopyIdentifierComponentSecondPass method doSecondPass.

@Override
public void doSecondPass(Map<String, PersistentClass> persistentClasses) throws MappingException {
    PersistentClass referencedPersistentClass = persistentClasses.get(referencedEntityName);
    // TODO better error names
    if (referencedPersistentClass == null) {
        throw new AnnotationException("Unknown entity name: " + referencedEntityName);
    }
    if (!(referencedPersistentClass.getIdentifier() instanceof Component)) {
        throw new AssertionFailure("Unexpected identifier type on the referenced entity when mapping a @MapsId: " + referencedEntityName);
    }
    Component referencedComponent = (Component) referencedPersistentClass.getIdentifier();
    // prepare column name structure
    boolean isExplicitReference = true;
    Map<String, AnnotatedJoinColumn> columnByReferencedName = CollectionHelper.mapOfSize(joinColumns.length);
    for (AnnotatedJoinColumn joinColumn : joinColumns) {
        final String referencedColumnName = joinColumn.getReferencedColumn();
        if (referencedColumnName == null || BinderHelper.isEmptyAnnotationValue(referencedColumnName)) {
            break;
        }
        // JPA 2 requires referencedColumnNames to be case insensitive
        columnByReferencedName.put(referencedColumnName.toLowerCase(Locale.ROOT), joinColumn);
    }
    // try default column orientation
    if (columnByReferencedName.isEmpty()) {
        isExplicitReference = false;
        for (int i = 0; i < joinColumns.length; i++) {
            columnByReferencedName.put(String.valueOf(i), joinColumns[i]);
        }
    }
    MutableInteger index = new MutableInteger();
    for (Property referencedProperty : referencedComponent.getProperties()) {
        if (referencedProperty.isComposite()) {
            Property property = createComponentProperty(referencedPersistentClass, isExplicitReference, columnByReferencedName, index, referencedProperty);
            component.addProperty(property);
        } else {
            Property property = createSimpleProperty(referencedPersistentClass, isExplicitReference, columnByReferencedName, index, referencedProperty);
            component.addProperty(property);
        }
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) MutableInteger(org.hibernate.internal.util.MutableInteger) AnnotationException(org.hibernate.AnnotationException) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 3 with MutableInteger

use of org.hibernate.internal.util.MutableInteger in project hibernate-orm by hibernate.

the class InlineDeleteHandler method execute.

@Override
public int execute(DomainQueryExecutionContext executionContext) {
    final List<Object> idsAndFks = MatchingIdSelectionHelper.selectMatchingIds(sqmDeleteStatement, domainParameterXref, executionContext);
    if (idsAndFks == null || idsAndFks.isEmpty()) {
        return 0;
    }
    final SessionFactoryImplementor factory = executionContext.getSession().getFactory();
    final String mutatingEntityName = sqmDeleteStatement.getTarget().getModel().getHibernateEntityName();
    final EntityMappingType entityDescriptor = factory.getRuntimeMetamodels().getEntityMappingType(mutatingEntityName);
    final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(domainParameterXref.getQueryParameterCount());
    // delete from the tables
    final MutableInteger valueIndexCounter = new MutableInteger();
    entityDescriptor.visitSubTypeAttributeMappings(attribute -> {
        if (attribute instanceof PluralAttributeMapping) {
            final PluralAttributeMapping pluralAttribute = (PluralAttributeMapping) attribute;
            if (pluralAttribute.getSeparateCollectionTable() != null) {
                // this collection has a separate collection table, meaning it is one of:
                // 1) element-collection
                // 2) many-to-many
                // 3) one-to many using a dedicated join-table
                // 
                // in all of these cases, we should clean up the matching rows in the
                // collection table
                final ModelPart fkTargetPart = pluralAttribute.getKeyDescriptor().getTargetPart();
                final int valueIndex;
                if (fkTargetPart instanceof EntityIdentifierMapping) {
                    valueIndex = 0;
                } else {
                    if (valueIndexCounter.get() == 0) {
                        valueIndexCounter.set(entityDescriptor.getIdentifierMapping().getJdbcTypeCount());
                    }
                    valueIndex = valueIndexCounter.get();
                    valueIndexCounter.plus(fkTargetPart.getJdbcTypeCount());
                }
                executeDelete(pluralAttribute.getSeparateCollectionTable(), entityDescriptor, () -> fkTargetPart::forEachSelectable, idsAndFks, valueIndex, fkTargetPart, jdbcParameterBindings, executionContext);
            }
        }
    });
    entityDescriptor.visitConstraintOrderedTables((tableExpression, tableKeyColumnsVisitationSupplier) -> {
        executeDelete(tableExpression, entityDescriptor, tableKeyColumnsVisitationSupplier, idsAndFks, 0, null, jdbcParameterBindings, executionContext);
    });
    return idsAndFks.size();
}
Also used : JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) ModelPart(org.hibernate.metamodel.mapping.ModelPart) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MutableInteger(org.hibernate.internal.util.MutableInteger) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Aggregations

MutableInteger (org.hibernate.internal.util.MutableInteger)3 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)2 JdbcParameterBindings (org.hibernate.sql.exec.spi.JdbcParameterBindings)2 AnnotationException (org.hibernate.AnnotationException)1 AssertionFailure (org.hibernate.AssertionFailure)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 Component (org.hibernate.mapping.Component)1 PersistentClass (org.hibernate.mapping.PersistentClass)1 Property (org.hibernate.mapping.Property)1 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)1 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)1 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)1 ModelPart (org.hibernate.metamodel.mapping.ModelPart)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 Joinable (org.hibernate.persister.entity.Joinable)1 SqmParameterMappingModelResolutionAccess (org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess)1 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)1 UnionTableReference (org.hibernate.sql.ast.tree.from.UnionTableReference)1 InSubQueryPredicate (org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate)1