use of org.hibernate.metamodel.mapping.ForeignKeyDescriptor in project hibernate-orm by hibernate.
the class MappingModelCreationHelper method setReferencedAttributeForeignKeyDescriptor.
private static void setReferencedAttributeForeignKeyDescriptor(AbstractAttributeMapping attributeMapping, ToOneAttributeMapping referencedAttributeMapping, EntityPersister referencedEntityDescriptor, String referencedPropertyName, Dialect dialect, MappingModelCreationProcess creationProcess) {
ForeignKeyDescriptor foreignKeyDescriptor = referencedAttributeMapping.getForeignKeyDescriptor();
if (foreignKeyDescriptor == null) {
PersistentClass entityBinding = creationProcess.getCreationContext().getBootModel().getEntityBinding(referencedEntityDescriptor.getEntityName());
Property property = entityBinding.getRecursiveProperty(referencedPropertyName);
interpretToOneKeyDescriptor(referencedAttributeMapping, property, (ToOne) property.getValue(), referencedAttributeMapping.getPropertyAccess(), dialect, creationProcess);
attributeMapping.setForeignKeyDescriptor(referencedAttributeMapping.getForeignKeyDescriptor());
} else {
attributeMapping.setForeignKeyDescriptor(foreignKeyDescriptor);
}
}
use of org.hibernate.metamodel.mapping.ForeignKeyDescriptor in project hibernate-orm by hibernate.
the class AbstractEmbeddableMapping method inverseInitializeCallback.
protected static boolean inverseInitializeCallback(TableGroupProducer declaringTableGroupProducer, SelectableMappings selectableMappings, EmbeddableMappingType inverseMappingType, MappingModelCreationProcess creationProcess, ManagedMappingType declaringType, List<? extends AttributeMapping> attributeMappings) {
if (inverseMappingType.getAttributeMappings().isEmpty()) {
return false;
}
// noinspection unchecked
final List<AttributeMapping> mappings = (List<AttributeMapping>) attributeMappings;
// Reset the attribute mappings that were added in previous attempts
mappings.clear();
int currentIndex = 0;
// We copy the attributes from the inverse mappings and replace the selection mappings
for (AttributeMapping attributeMapping : inverseMappingType.getAttributeMappings()) {
if (attributeMapping instanceof BasicAttributeMapping) {
final BasicAttributeMapping original = (BasicAttributeMapping) attributeMapping;
final SelectableMapping selectableMapping = selectableMappings.getSelectable(currentIndex);
attributeMapping = BasicAttributeMapping.withSelectableMapping(declaringType, original, original.getPropertyAccess(), original.getValueGeneration(), selectableMapping);
currentIndex++;
} else if (attributeMapping instanceof ToOneAttributeMapping) {
final ToOneAttributeMapping original = (ToOneAttributeMapping) attributeMapping;
ForeignKeyDescriptor foreignKeyDescriptor = original.getForeignKeyDescriptor();
if (foreignKeyDescriptor == null) {
// and the callback is re-queued.
throw new IllegalStateException("Not yet ready: " + original);
}
final ToOneAttributeMapping toOne = original.copy(declaringType, declaringTableGroupProducer);
final int offset = currentIndex;
toOne.setIdentifyingColumnsTableExpression(selectableMappings.getSelectable(offset).getContainingTableExpression());
toOne.setForeignKeyDescriptor(foreignKeyDescriptor.withKeySelectionMapping(declaringType, declaringTableGroupProducer, index -> selectableMappings.getSelectable(offset + index), creationProcess));
attributeMapping = toOne;
currentIndex += attributeMapping.getJdbcTypeCount();
} else if (attributeMapping instanceof EmbeddableValuedModelPart) {
final SelectableMapping[] subMappings = new SelectableMapping[attributeMapping.getJdbcTypeCount()];
for (int i = 0; i < subMappings.length; i++) {
subMappings[i] = selectableMappings.getSelectable(currentIndex++);
}
attributeMapping = MappingModelCreationHelper.createInverseModelPart((EmbeddableValuedModelPart) attributeMapping, declaringType, declaringTableGroupProducer, new SelectableMappingsImpl(subMappings), creationProcess);
} else {
throw new UnsupportedMappingException("Only basic and to-one attributes are supported in composite fks");
}
mappings.add(attributeMapping);
}
return true;
}
use of org.hibernate.metamodel.mapping.ForeignKeyDescriptor in project hibernate-orm by hibernate.
the class SimpleDeleteQueryPlan method executeUpdate.
@Override
public int executeUpdate(DomainQueryExecutionContext executionContext) {
BulkOperationCleanupAction.schedule(executionContext.getSession(), sqmDelete);
final SharedSessionContractImplementor session = executionContext.getSession();
final SessionFactoryImplementor factory = session.getFactory();
final JdbcServices jdbcServices = factory.getJdbcServices();
SqlAstTranslator<JdbcDelete> deleteTranslator = null;
if (jdbcDelete == null) {
deleteTranslator = createDeleteTranslator(executionContext);
}
final JdbcParameterBindings jdbcParameterBindings = SqmUtil.createJdbcParameterBindings(executionContext.getQueryParameterBindings(), domainParameterXref, jdbcParamsXref, factory.getRuntimeMetamodels().getMappingMetamodel(), sqmInterpretation.getFromClauseAccess()::findTableGroup, new SqmParameterMappingModelResolutionAccess() {
@Override
@SuppressWarnings("unchecked")
public <T> MappingModelExpressible<T> getResolvedMappingModelType(SqmParameter<T> parameter) {
return (MappingModelExpressible<T>) sqmInterpretation.getSqmParameterMappingModelTypeResolutions().get(parameter);
}
}, session);
if (jdbcDelete != null && !jdbcDelete.isCompatibleWith(jdbcParameterBindings, executionContext.getQueryOptions())) {
deleteTranslator = createDeleteTranslator(executionContext);
}
if (deleteTranslator != null) {
jdbcDelete = deleteTranslator.translate(jdbcParameterBindings, executionContext.getQueryOptions());
} else {
jdbcDelete.bindFilterJdbcParameters(jdbcParameterBindings);
}
final boolean missingRestriction = sqmDelete.getWhereClause() == null || sqmDelete.getWhereClause().getPredicate() == null;
if (missingRestriction) {
assert domainParameterXref.getSqmParameterCount() == 0;
assert jdbcParamsXref.isEmpty();
}
final SqmJdbcExecutionContextAdapter executionContextAdapter = SqmJdbcExecutionContextAdapter.usingLockingAndPaging(executionContext);
SqmMutationStrategyHelper.cleanUpCollectionTables(entityDescriptor, (tableReference, attributeMapping) -> {
if (missingRestriction) {
return null;
}
final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
final Expression fkColumnExpression = MappingModelHelper.buildColumnReferenceExpression(fkDescriptor.getKeyPart(), null, factory);
final QuerySpec matchingIdSubQuery = new QuerySpec(false);
final MutatingTableReferenceGroupWrapper tableGroup = new MutatingTableReferenceGroupWrapper(new NavigablePath(attributeMapping.getRootPathName()), attributeMapping, sqmInterpretation.getSqlAst().getTargetTable());
final Expression fkTargetColumnExpression = MappingModelHelper.buildColumnReferenceExpression(tableGroup, fkDescriptor.getTargetPart(), sqmInterpretation.getSqlExpressionResolver(), factory);
matchingIdSubQuery.getSelectClause().addSqlSelection(new SqlSelectionImpl(1, 0, fkTargetColumnExpression));
matchingIdSubQuery.getFromClause().addRoot(tableGroup);
matchingIdSubQuery.applyPredicate(sqmInterpretation.getSqlAst().getRestriction());
return new InSubQueryPredicate(fkColumnExpression, matchingIdSubQuery, false);
}, (missingRestriction ? JdbcParameterBindings.NO_BINDINGS : jdbcParameterBindings), executionContextAdapter);
return jdbcServices.getJdbcMutationExecutor().execute(jdbcDelete, jdbcParameterBindings, sql -> session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql), (integer, preparedStatement) -> {
}, executionContextAdapter);
}
use of org.hibernate.metamodel.mapping.ForeignKeyDescriptor in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method applySubSelectRestriction.
private void applySubSelectRestriction(QuerySpec querySpec, NavigablePath rootNavigablePath, TableGroup rootTableGroup, SubselectFetch subselect, LoaderSqlAstCreationState sqlAstCreationState) {
final SqlAstCreationContext sqlAstCreationContext = sqlAstCreationState.getCreationContext();
final SessionFactoryImplementor sessionFactory = sqlAstCreationContext.getSessionFactory();
assert loadable instanceof PluralAttributeMapping;
final PluralAttributeMapping attributeMapping = (PluralAttributeMapping) loadable;
final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
final NavigablePath navigablePath = rootNavigablePath.append(attributeMapping.getAttributeName());
final Expression fkExpression;
final int jdbcTypeCount = fkDescriptor.getJdbcTypeCount();
if (jdbcTypeCount == 1) {
assert fkDescriptor instanceof SimpleForeignKeyDescriptor;
final SimpleForeignKeyDescriptor simpleFkDescriptor = (SimpleForeignKeyDescriptor) fkDescriptor;
final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, simpleFkDescriptor.getContainingTableExpression());
fkExpression = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(createColumnReferenceKey(tableReference, simpleFkDescriptor.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, simpleFkDescriptor.getSelectionExpression(), false, null, null, simpleFkDescriptor.getJdbcMapping(), this.creationContext.getSessionFactory()));
} else {
final List<ColumnReference> columnReferences = new ArrayList<>(jdbcTypeCount);
fkDescriptor.forEachSelectable((columnIndex, selection) -> {
final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
columnReferences.add((ColumnReference) sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, this.creationContext.getSessionFactory())));
});
fkExpression = new SqlTuple(columnReferences, fkDescriptor);
}
querySpec.applyPredicate(new InSubQueryPredicate(fkExpression, generateSubSelect(attributeMapping, rootTableGroup, subselect, jdbcTypeCount, sqlAstCreationState, sessionFactory), false));
}
use of org.hibernate.metamodel.mapping.ForeignKeyDescriptor in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method generateSubSelect.
private QueryPart generateSubSelect(PluralAttributeMapping attributeMapping, TableGroup rootTableGroup, SubselectFetch subselect, int jdbcTypeCount, LoaderSqlAstCreationState creationState, SessionFactoryImplementor sessionFactory) {
final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
final QuerySpec subQuery = new QuerySpec(false);
final QuerySpec loadingSqlAst = subselect.getLoadingSqlAst();
// todo (6.0) : we need to find the owner's TableGroup in the `loadingSqlAst`
final TableGroup ownerTableGroup = subselect.getOwnerTableGroup();
// transfer the from-clause
loadingSqlAst.getFromClause().visitRoots(subQuery.getFromClause()::addRoot);
final SqlExpressionResolver sqlExpressionResolver = creationState.getSqlExpressionResolver();
final NavigablePath navigablePath = ownerTableGroup.getNavigablePath().append(attributeMapping.getAttributeName());
fkDescriptor.visitTargetSelectables((valuesPosition, selection) -> {
// for each column, resolve a SqlSelection and add it to the sub-query select-clause
final TableReference tableReference = ownerTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final Expression expression = sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, sessionFactory));
subQuery.getSelectClause().addSqlSelection(new SqlSelectionImpl(valuesPosition + 1, valuesPosition, expression));
});
// transfer the restriction
subQuery.applyPredicate(loadingSqlAst.getWhereClauseRestrictions());
return subQuery;
}
Aggregations