Search in sources :

Example 1 with JdbcValuesMetadata

use of org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata in project hibernate-orm by hibernate.

the class DynamicResultBuilderBasicConverted method buildResult.

@Override
public BasicResult<?> buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final TypeConfiguration typeConfiguration = domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration();
    final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(columnAlias, state -> {
        final int currentJdbcPosition = resultPosition + 1;
        final int jdbcPosition;
        if (columnAlias != null) {
            jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
        } else {
            jdbcPosition = currentJdbcPosition;
        }
        final BasicType<?> basicType = jdbcResultsMetadata.resolveType(jdbcPosition, basicValueConverter.getRelationalJavaType(), domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory());
        final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
        return new ResultSetMappingSqlSelection(valuesArrayPosition, (BasicValuedMapping) basicType);
    }), basicValueConverter.getDomainJavaType(), typeConfiguration);
    return new BasicResult<>(sqlSelection.getValuesArrayPosition(), columnAlias, basicValueConverter.getDomainJavaType(), basicValueConverter);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) JpaAttributeConverterImpl(org.hibernate.metamodel.model.convert.internal.JpaAttributeConverterImpl) ProvidedInstanceManagedBeanImpl(org.hibernate.resource.beans.spi.ProvidedInstanceManagedBeanImpl) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) BasicType(org.hibernate.type.BasicType) BiFunction(java.util.function.BiFunction) JavaType(org.hibernate.type.descriptor.java.JavaType) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Objects(java.util.Objects) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) ResultsHelper(org.hibernate.query.results.ResultsHelper) ManagedBean(org.hibernate.resource.beans.spi.ManagedBean) ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) BasicValueConverter(org.hibernate.metamodel.model.convert.spi.BasicValueConverter) AttributeConverter(jakarta.persistence.AttributeConverter) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) BasicType(org.hibernate.type.BasicType) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 2 with JdbcValuesMetadata

use of org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata in project hibernate-orm by hibernate.

the class DynamicResultBuilderEntityStandard method resolveSqlSelection.

private void resolveSqlSelection(String columnAlias, String columnKey, JdbcMapping jdbcMapping, JdbcValuesMetadata jdbcResultsMetadata, DomainResultCreationState domainResultCreationState) {
    final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
    sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(columnKey, state -> {
        final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
        final int valuesArrayPosition = jdbcPosition - 1;
        return new ResultSetMappingSqlSelection(valuesArrayPosition, jdbcMapping);
    }), jdbcMapping.getMappedJavaType(), domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration());
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) SingleAttributeIdentifierMapping(org.hibernate.metamodel.mapping.internal.SingleAttributeIdentifierMapping) TableGroupImpl(org.hibernate.query.results.TableGroupImpl) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) ArrayList(java.util.ArrayList) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) NativeQuery(org.hibernate.query.NativeQuery) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) Fetchable(org.hibernate.sql.results.graph.Fetchable) FetchTiming(org.hibernate.engine.FetchTiming) LockMode(org.hibernate.LockMode) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) Fetch(org.hibernate.sql.results.graph.Fetch) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Objects(java.util.Objects) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) List(java.util.List) FetchBuilder(org.hibernate.query.results.FetchBuilder) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) Collections(java.util.Collections) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqlExpressionResolver.createColumnReferenceKey(org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey) FetchParent(org.hibernate.sql.results.graph.FetchParent) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection)

Example 3 with JdbcValuesMetadata

use of org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata in project hibernate-orm by hibernate.

the class DynamicFetchBuilderStandard method buildFetch.

@Override
public Fetch buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl(domainResultCreationState);
    final TableGroup ownerTableGroup = creationStateImpl.getFromClauseAccess().getTableGroup(parent.getNavigablePath());
    final Fetchable attributeMapping = (Fetchable) parent.getReferencedMappingContainer().findSubPart(fetchableName, null);
    final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
    final SelectableConsumer selectableConsumer = (selectionIndex, selectableMapping) -> {
        final TableReference tableReference = ownerTableGroup.resolveTableReference(fetchPath, selectableMapping.getContainingTableExpression());
        final String columnAlias = columnNames.get(selectionIndex);
        sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selectableMapping.getSelectionExpression()), state -> {
            final int resultSetPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
            final int valuesArrayPosition = resultSetPosition - 1;
            return new ResultSetMappingSqlSelection(valuesArrayPosition, selectableMapping.getJdbcMapping());
        }), selectableMapping.getJdbcMapping().getMappedJavaType(), domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration());
    };
    if (attributeMapping instanceof BasicValuedMapping) {
        attributeMapping.forEachSelectable(selectableConsumer);
        return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.IMMEDIATE, true, null, creationStateImpl);
    } else if (attributeMapping instanceof EmbeddedAttributeMapping) {
        attributeMapping.forEachSelectable(selectableConsumer);
        return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.IMMEDIATE, false, null, creationStateImpl);
    } else if (attributeMapping instanceof ToOneAttributeMapping) {
        final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
        toOneAttributeMapping.getForeignKeyDescriptor().getPart(toOneAttributeMapping.getSideNature()).forEachSelectable(selectableConsumer);
        return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.DELAYED, false, null, creationStateImpl);
    } else {
        assert attributeMapping instanceof PluralAttributeMapping;
        final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) attributeMapping;
        pluralAttributeMapping.getKeyDescriptor().visitTargetSelectables(selectableConsumer);
        return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.DELAYED, false, null, creationStateImpl);
    }
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) Fetchable(org.hibernate.sql.results.graph.Fetchable) FetchTiming(org.hibernate.engine.FetchTiming) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) Fetch(org.hibernate.sql.results.graph.Fetch) ArrayList(java.util.ArrayList) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) TableReference(org.hibernate.sql.ast.tree.from.TableReference) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ResultsHelper(org.hibernate.query.results.ResultsHelper) List(java.util.List) NativeQuery(org.hibernate.query.NativeQuery) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) SqlExpressionResolver.createColumnReferenceKey(org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey) FetchParent(org.hibernate.sql.results.graph.FetchParent) Fetchable(org.hibernate.sql.results.graph.Fetchable) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) TableReference(org.hibernate.sql.ast.tree.from.TableReference) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver)

Example 4 with JdbcValuesMetadata

use of org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata in project hibernate-orm by hibernate.

the class CompleteResultBuilderBasicModelPart method buildResult.

@Override
public BasicResult<?> buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationStateImpl = impl(domainResultCreationState);
    final TableGroup tableGroup = creationStateImpl.getFromClauseAccess().getTableGroup(navigablePath.getParent());
    final TableReference tableReference = tableGroup.resolveTableReference(navigablePath, modelPart.getContainingTableExpression());
    final String mappedColumn = modelPart.getSelectionExpression();
    final SqlSelection sqlSelection = creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, mappedColumn), processingState -> {
        final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
        final int valuesArrayPosition = jdbcPositionToValuesArrayPosition(jdbcPosition);
        return new ResultSetMappingSqlSelection(valuesArrayPosition, modelPart);
    }), modelPart.getJavaType(), creationStateImpl.getSessionFactory().getTypeConfiguration());
    // noinspection unchecked
    return new BasicResult(sqlSelection.getValuesArrayPosition(), columnAlias, modelPart.getJavaType());
}
Also used : BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) ResultsHelper.jdbcPositionToValuesArrayPosition(org.hibernate.query.results.ResultsHelper.jdbcPositionToValuesArrayPosition) ResultBuilder(org.hibernate.query.results.ResultBuilder) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) TableReference(org.hibernate.sql.ast.tree.from.TableReference) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 5 with JdbcValuesMetadata

use of org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata in project hibernate-orm by hibernate.

the class DynamicFetchBuilderLegacy method resolveSqlSelection.

private void resolveSqlSelection(String columnAlias, String columnKey, JdbcMapping jdbcMapping, JdbcValuesMetadata jdbcResultsMetadata, DomainResultCreationState domainResultCreationState) {
    final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
    sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(columnKey, state -> {
        final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
        final int valuesArrayPosition = jdbcPosition - 1;
        return new ResultSetMappingSqlSelection(valuesArrayPosition, jdbcMapping);
    }), jdbcMapping.getMappedJavaType(), domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration());
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BiFunction(java.util.function.BiFunction) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) HashMap(java.util.HashMap) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ResultsHelper(org.hibernate.query.results.ResultsHelper) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Map(java.util.Map) NativeQuery(org.hibernate.query.NativeQuery) BiConsumer(java.util.function.BiConsumer) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) FetchTiming(org.hibernate.engine.FetchTiming) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) LockMode(org.hibernate.LockMode) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) SqlAliasBase(org.hibernate.sql.ast.spi.SqlAliasBase) Fetch(org.hibernate.sql.results.graph.Fetch) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Objects(java.util.Objects) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) List(java.util.List) FetchBuilder(org.hibernate.query.results.FetchBuilder) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqlExpressionResolver.createColumnReferenceKey(org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey) FetchParent(org.hibernate.sql.results.graph.FetchParent) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection)

Aggregations

JdbcValuesMetadata (org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata)12 BiFunction (java.util.function.BiFunction)11 ResultSetMappingSqlSelection (org.hibernate.query.results.ResultSetMappingSqlSelection)11 DomainResultCreationState (org.hibernate.sql.results.graph.DomainResultCreationState)11 DomainResultCreationStateImpl (org.hibernate.query.results.DomainResultCreationStateImpl)9 SqlExpressionResolver (org.hibernate.sql.ast.spi.SqlExpressionResolver)9 ResultsHelper.impl (org.hibernate.query.results.ResultsHelper.impl)7 NavigablePath (org.hibernate.query.spi.NavigablePath)7 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)7 ResultsHelper (org.hibernate.query.results.ResultsHelper)6 DynamicFetchBuilderLegacy (org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy)6 List (java.util.List)5 Objects (java.util.Objects)5 FetchTiming (org.hibernate.engine.FetchTiming)5 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)5 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)5 NativeQuery (org.hibernate.query.NativeQuery)5 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)5 BasicResult (org.hibernate.sql.results.graph.basic.BasicResult)5 FetchBuilder (org.hibernate.query.results.FetchBuilder)4