use of org.hibernate.metamodel.mapping.ForeignKeyDescriptor in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitIsEmptyPredicate.
@Override
public Object visitIsEmptyPredicate(SqmEmptinessPredicate predicate) {
prepareReusablePath(predicate.getPluralPath(), () -> null);
final QuerySpec subQuerySpec = new QuerySpec(false, 1);
final FromClauseAccess parentFromClauseAccess = getFromClauseAccess();
final SqlAstProcessingStateImpl subQueryState = new SqlAstProcessingStateImpl(getCurrentProcessingState(), this, currentClauseStack::getCurrent);
pushProcessingState(subQueryState);
try {
final SqmPluralValuedSimplePath<?> sqmPluralPath = predicate.getPluralPath();
final NavigablePath pluralPathNavPath = sqmPluralPath.getNavigablePath();
final NavigablePath parentNavPath = pluralPathNavPath.getParent();
assert parentNavPath != null;
final TableGroup parentTableGroup = parentFromClauseAccess.getTableGroup(parentNavPath);
final SqlAliasBase sqlAliasBase = sqlAliasBaseManager.createSqlAliasBase(parentTableGroup.getGroupAlias());
final TableGroup tableGroup = new CorrelatedTableGroup(parentTableGroup, sqlAliasBase, subQuerySpec, subQuerySpec::applyPredicate, creationContext.getSessionFactory());
subQueryState.getSqlAstCreationState().getFromClauseAccess().registerTableGroup(parentNavPath, tableGroup);
registerPluralTableGroupParts(tableGroup);
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) visitPluralValuedPath(sqmPluralPath).getExpressionType();
// The creation of the table group join against the correlated table group
// has the side effect that the from and where clause of the sub-query are set
tableGroup.addTableGroupJoin(pluralAttributeMapping.createTableGroupJoin(pluralPathNavPath, tableGroup, sqmPluralPath.getExplicitAlias(), SqlAstJoinType.INNER, false, false, sqlAliasBaseManager, subQueryState, this, creationContext));
final ForeignKeyDescriptor collectionKeyDescriptor = pluralAttributeMapping.getKeyDescriptor();
final int jdbcTypeCount = collectionKeyDescriptor.getJdbcTypeCount();
assert jdbcTypeCount > 0;
final JdbcLiteral<Integer> jdbcLiteral = new JdbcLiteral<>(1, basicType(Integer.class));
subQuerySpec.getSelectClause().addSqlSelection(new SqlSelectionImpl(1, 0, jdbcLiteral));
return new ExistsPredicate(subQuerySpec, !predicate.isNegated(), getBooleanType());
} finally {
popProcessingStateStack();
}
}
use of org.hibernate.metamodel.mapping.ForeignKeyDescriptor 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.metamodel.mapping.ForeignKeyDescriptor in project hibernate-orm by hibernate.
the class RestrictedDeleteExecutionDelegate method executeUsingIdTable.
private int executeUsingIdTable(Predicate predicate, ExecutionContext executionContext, JdbcParameterBindings jdbcParameterBindings) {
final int rows = ExecuteWithTemporaryTableHelper.saveMatchingIdsIntoIdTable(converter, predicate, idTable, sessionUidAccess, jdbcParameterBindings, executionContext);
final QuerySpec idTableIdentifierSubQuery = ExecuteWithTemporaryTableHelper.createIdTableSelectQuerySpec(idTable, sessionUidAccess, entityDescriptor, executionContext);
SqmMutationStrategyHelper.cleanUpCollectionTables(entityDescriptor, (tableReference, attributeMapping) -> {
final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
final QuerySpec idTableFkSubQuery;
if (fkDescriptor.getTargetPart() instanceof EntityIdentifierMapping) {
idTableFkSubQuery = idTableIdentifierSubQuery;
} else {
idTableFkSubQuery = ExecuteWithTemporaryTableHelper.createIdTableSelectQuerySpec(idTable, fkDescriptor.getTargetPart(), sessionUidAccess, entityDescriptor, executionContext);
}
return new InSubQueryPredicate(MappingModelHelper.buildColumnReferenceExpression(fkDescriptor, null, sessionFactory), idTableFkSubQuery, false);
}, JdbcParameterBindings.NO_BINDINGS, executionContext);
entityDescriptor.visitConstraintOrderedTables((tableExpression, tableKeyColumnVisitationSupplier) -> deleteFromTableUsingIdTable(tableExpression, tableKeyColumnVisitationSupplier, idTableIdentifierSubQuery, executionContext));
return rows;
}
use of org.hibernate.metamodel.mapping.ForeignKeyDescriptor in project hibernate-orm by hibernate.
the class EntityWithOneBidirectionalJoinTableAssociationTest method basicTest.
@Test
public void basicTest(SessionFactoryScope scope) {
final EntityPersister parentDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(Parent.class);
final ModelPart childAssociation = parentDescriptor.findSubPart("child");
assertThat(childAssociation, instanceOf(ToOneAttributeMapping.class));
final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) childAssociation;
ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("PARENT_CHILD"));
assertThat(selection.getSelectionExpression(), is("child_id"));
});
foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("CHILD"));
assertThat(selection.getSelectionExpression(), is("id"));
});
final EntityPersister childDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(Child.class);
final ModelPart parentAssociation = childDescriptor.findSubPart("parent");
assertThat(parentAssociation, instanceOf(ToOneAttributeMapping.class));
final ToOneAttributeMapping parentAttributeMapping = (ToOneAttributeMapping) parentAssociation;
foreignKeyDescriptor = parentAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("PARENT_CHILD"));
assertThat(selection.getSelectionExpression(), is("parent_id"));
});
foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("PARENT"));
assertThat(selection.getSelectionExpression(), is("id"));
});
}
use of org.hibernate.metamodel.mapping.ForeignKeyDescriptor in project hibernate-orm by hibernate.
the class EntityWithBidirectionalAssociationTest method basicTest.
@Test
public void basicTest(SessionFactoryScope scope) {
final EntityPersister parentDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(Parent.class);
final ModelPart childAssociation = parentDescriptor.findSubPart("child");
assertThat(childAssociation, instanceOf(ToOneAttributeMapping.class));
final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) childAssociation;
ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("PARENT"));
assertThat(selection.getSelectionExpression(), is("child_id"));
});
foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("CHILD"));
assertThat(selection.getSelectionExpression(), is("id"));
});
final EntityPersister childDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(Child.class);
final ModelPart parentAssociation = childDescriptor.findSubPart("parent");
assertThat(parentAssociation, instanceOf(ToOneAttributeMapping.class));
final ToOneAttributeMapping parentAttributeMapping = (ToOneAttributeMapping) parentAssociation;
foreignKeyDescriptor = parentAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("PARENT"));
assertThat(selection.getSelectionExpression(), is("child_id"));
});
foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
assertThat(selection.getContainingTableExpression(), is("CHILD"));
assertThat(selection.getSelectionExpression(), is("id"));
});
}
Aggregations