use of org.hibernate.metamodel.mapping.SelectableMappings in project hibernate-orm by hibernate.
the class AbstractCompositeIdentifierMapping method toSqlExpression.
@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
final SelectableMappings selectableMappings = getEmbeddableTypeDescriptor();
final List<ColumnReference> columnReferences = CollectionHelper.arrayList(selectableMappings.getJdbcTypeCount());
final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
getEmbeddableTypeDescriptor().forEachSelectable((columnIndex, selection) -> {
final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
columnReferences.add((ColumnReference) columnReference);
});
return new SqlTuple(columnReferences, this);
}
use of org.hibernate.metamodel.mapping.SelectableMappings in project hibernate-orm by hibernate.
the class MappingModelCreationHelper method buildEmbeddableForeignKeyDescriptor.
public static EmbeddedForeignKeyDescriptor buildEmbeddableForeignKeyDescriptor(EmbeddableValuedModelPart embeddableValuedModelPart, Value bootValueMapping, ManagedMappingType keyDeclaringType, TableGroupProducer keyDeclaringTableGroupProducer, boolean inverse, Dialect dialect, MappingModelCreationProcess creationProcess) {
final boolean hasConstraint;
final SelectableMappings keySelectableMappings;
final String keyTableExpression;
if (bootValueMapping instanceof Collection) {
final Collection collectionBootValueMapping = (Collection) bootValueMapping;
hasConstraint = ((SimpleValue) collectionBootValueMapping.getKey()).isConstrained();
keyTableExpression = getTableIdentifierExpression(collectionBootValueMapping.getCollectionTable(), creationProcess);
keySelectableMappings = SelectableMappingsImpl.from(keyTableExpression, collectionBootValueMapping.getKey(), getPropertyOrder(bootValueMapping, creationProcess), creationProcess.getCreationContext().getSessionFactory(), dialect, creationProcess.getSqmFunctionRegistry());
} else {
if (bootValueMapping instanceof OneToMany) {
// We assume there is a constraint if the mapping is not nullable
hasConstraint = !bootValueMapping.isNullable();
} else {
hasConstraint = ((SimpleValue) bootValueMapping).isConstrained();
}
keyTableExpression = getTableIdentifierExpression(bootValueMapping.getTable(), creationProcess);
keySelectableMappings = SelectableMappingsImpl.from(keyTableExpression, bootValueMapping, getPropertyOrder(bootValueMapping, creationProcess), creationProcess.getCreationContext().getSessionFactory(), dialect, creationProcess.getSqmFunctionRegistry());
}
if (inverse) {
return new EmbeddedForeignKeyDescriptor(embeddableValuedModelPart, createInverseModelPart(embeddableValuedModelPart, keyDeclaringType, keyDeclaringTableGroupProducer, keySelectableMappings, creationProcess), embeddableValuedModelPart.getContainingTableExpression(), embeddableValuedModelPart.getEmbeddableTypeDescriptor(), keyTableExpression, keySelectableMappings, hasConstraint, creationProcess);
} else {
return new EmbeddedForeignKeyDescriptor(createInverseModelPart(embeddableValuedModelPart, keyDeclaringType, keyDeclaringTableGroupProducer, keySelectableMappings, creationProcess), embeddableValuedModelPart, keyTableExpression, keySelectableMappings, embeddableValuedModelPart.getContainingTableExpression(), embeddableValuedModelPart.getEmbeddableTypeDescriptor(), hasConstraint, creationProcess);
}
}
use of org.hibernate.metamodel.mapping.SelectableMappings in project hibernate-orm by hibernate.
the class NonAggregatedIdentifierMappingImpl method toSqlExpression.
@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
if (hasContainingClass()) {
final SelectableMappings selectableMappings = getEmbeddableTypeDescriptor();
final List<ColumnReference> columnReferences = CollectionHelper.arrayList(selectableMappings.getJdbcTypeCount());
final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
int offset = 0;
for (AttributeMapping attributeMapping : identifierValueMapper.getAttributeMappings()) {
offset += attributeMapping.forEachSelectable(offset, (columnIndex, selection) -> {
final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
columnReferences.add((ColumnReference) columnReference);
});
}
return new SqlTuple(columnReferences, this);
}
return super.toSqlExpression(tableGroup, clause, walker, sqlAstCreationState);
}
use of org.hibernate.metamodel.mapping.SelectableMappings in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method resolveSqmParameter.
private void resolveSqmParameter(SqmParameter<?> expression, MappingModelExpressible<?> valueMapping, BiConsumer<Integer, JdbcParameter> jdbcParameterConsumer) {
sqmParameterMappingModelTypes.put(expression, valueMapping);
final Bindable bindable;
if (valueMapping instanceof EntityAssociationMapping) {
final EntityAssociationMapping mapping = (EntityAssociationMapping) valueMapping;
bindable = mapping.getForeignKeyDescriptor().getPart(mapping.getSideNature());
} else if (valueMapping instanceof EntityMappingType) {
bindable = ((EntityMappingType) valueMapping).getIdentifierMapping();
} else {
bindable = valueMapping;
}
if (bindable instanceof SelectableMappings) {
((SelectableMappings) bindable).forEachSelectable((index, selectableMapping) -> jdbcParameterConsumer.accept(index, new SqlTypedMappingJdbcParameter(selectableMapping)));
} else if (bindable instanceof SelectableMapping) {
jdbcParameterConsumer.accept(0, new SqlTypedMappingJdbcParameter((SelectableMapping) bindable));
} else {
SqlTypedMapping sqlTypedMapping = null;
if (bindable instanceof BasicType<?>) {
final int sqlTypeCode = ((BasicType<?>) bindable).getJdbcType().getDefaultSqlTypeCode();
if (sqlTypeCode == SqlTypes.NUMERIC || sqlTypeCode == SqlTypes.DECIMAL) {
// For numeric and decimal parameter types we must determine the precision/scale of the value.
// When we need to cast the parameter later, it is necessary to know the size to avoid truncation.
final QueryParameterBinding<?> binding = domainParameterBindings.getBinding(domainParameterXref.getQueryParameter(expression));
final Object bindValue = binding.getBindValue();
if (bindValue != null) {
if (bindValue instanceof BigInteger) {
int precision = bindValue.toString().length() - (((BigInteger) bindValue).signum() < 0 ? 1 : 0);
sqlTypedMapping = new SqlTypedMappingImpl(null, null, precision, 0, ((BasicType<?>) bindable).getJdbcMapping());
} else if (bindValue instanceof BigDecimal) {
final BigDecimal bigDecimal = (BigDecimal) bindValue;
sqlTypedMapping = new SqlTypedMappingImpl(null, null, bigDecimal.precision(), bigDecimal.scale(), ((BasicType<?>) bindable).getJdbcMapping());
}
}
}
}
if (sqlTypedMapping == null) {
bindable.forEachJdbcType((index, jdbcMapping) -> jdbcParameterConsumer.accept(index, new JdbcParameterImpl(jdbcMapping)));
} else {
jdbcParameterConsumer.accept(0, new SqlTypedMappingJdbcParameter(sqlTypedMapping));
}
}
}
Aggregations