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);
}
}
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);
}
}
}
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();
}
Aggregations