Search in sources :

Example 1 with Assignable

use of org.hibernate.sql.ast.tree.update.Assignable in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitInsertionTargetPaths.

public AdditionalInsertValues visitInsertionTargetPaths(BiConsumer<Assignable, List<ColumnReference>> targetColumnReferenceConsumer, SqmInsertStatement<?> sqmStatement, EntityPersister entityDescriptor, TableGroup rootTableGroup) {
    final List<SqmPath<?>> targetPaths = sqmStatement.getInsertionTargetPaths();
    final EntityDiscriminatorMapping discriminatorMapping = entityDescriptor.getDiscriminatorMapping();
    IdentifierGenerator identifierGenerator = entityDescriptor.getIdentifierGenerator();
    Expression versionExpression = null;
    Expression discriminatorExpression = null;
    BasicEntityIdentifierMapping identifierMapping = null;
    // We use the id property name to null the identifier generator variable if the target paths contain the id
    final String identifierPropertyName;
    if (identifierGenerator != null) {
        identifierPropertyName = entityDescriptor.getIdentifierPropertyName();
    } else {
        identifierPropertyName = null;
    }
    final String versionAttributeName;
    boolean needsVersionInsert;
    if (entityDescriptor.isVersioned()) {
        versionAttributeName = entityDescriptor.getVersionMapping().getVersionAttribute().getAttributeName();
        needsVersionInsert = true;
    } else {
        versionAttributeName = null;
        needsVersionInsert = false;
    }
    // Go through all target paths and remember if the target paths contain the version or id attributes
    for (int i = 0; i < targetPaths.size(); i++) {
        final SqmPath<?> path = targetPaths.get(i);
        final String localName = path.getNavigablePath().getLocalName();
        if (localName.equals(identifierPropertyName)) {
            identifierGenerator = null;
        } else if (localName.equals(versionAttributeName)) {
            needsVersionInsert = false;
        }
        final Assignable assignable = (Assignable) path.accept(this);
        targetColumnReferenceConsumer.accept(assignable, assignable.getColumnReferences());
    }
    if (needsVersionInsert) {
        final BasicValuedPathInterpretation<?> versionPath = BasicValuedPathInterpretation.from((SqmBasicValuedSimplePath<?>) sqmStatement.getTarget().get(versionAttributeName), this, this, jpaQueryComplianceEnabled);
        final List<ColumnReference> targetColumnReferences = versionPath.getColumnReferences();
        assert targetColumnReferences.size() == 1;
        targetColumnReferenceConsumer.accept(versionPath, targetColumnReferences);
        versionExpression = new VersionTypeSeedParameterSpecification(entityDescriptor.getVersionMapping().getJdbcMapping(), entityDescriptor.getVersionJavaType());
    }
    if (discriminatorMapping != null && discriminatorMapping.isPhysical()) {
        final BasicValuedPathInterpretation<?> discriminatorPath = new BasicValuedPathInterpretation<>(new ColumnReference(rootTableGroup.resolveTableReference(discriminatorMapping.getContainingTableExpression()), discriminatorMapping, getCreationContext().getSessionFactory()), rootTableGroup.getNavigablePath().append(discriminatorMapping.getPartName()), discriminatorMapping, rootTableGroup);
        targetColumnReferenceConsumer.accept(discriminatorPath, discriminatorPath.getColumnReferences());
        discriminatorExpression = new QueryLiteral<>(entityDescriptor.getDiscriminatorValue(), discriminatorMapping);
    }
    // This uses identity generation, so we don't need to list the column
    if (identifierGenerator instanceof PostInsertIdentifierGenerator || identifierGenerator instanceof CompositeNestedGeneratedValueGenerator) {
        identifierGenerator = null;
    } else if (identifierGenerator != null) {
        identifierMapping = (BasicEntityIdentifierMapping) entityDescriptor.getIdentifierMapping();
        final BasicValuedPathInterpretation<?> identifierPath = new BasicValuedPathInterpretation<>(new ColumnReference(rootTableGroup.resolveTableReference(identifierMapping.getContainingTableExpression()), identifierMapping, getCreationContext().getSessionFactory()), rootTableGroup.getNavigablePath().append(identifierMapping.getPartName()), identifierMapping, rootTableGroup);
        targetColumnReferenceConsumer.accept(identifierPath, identifierPath.getColumnReferences());
    }
    return new AdditionalInsertValues(versionExpression, discriminatorExpression, identifierGenerator, identifierMapping);
}
Also used : CompositeNestedGeneratedValueGenerator(org.hibernate.id.CompositeNestedGeneratedValueGenerator) VersionTypeSeedParameterSpecification(org.hibernate.sql.exec.internal.VersionTypeSeedParameterSpecification) BasicValuedPathInterpretation(org.hibernate.query.sqm.sql.internal.BasicValuedPathInterpretation) SelfInterpretingSqmPath(org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) DiscriminatorSqmPath(org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath) BasicEntityIdentifierMapping(org.hibernate.metamodel.mapping.BasicEntityIdentifierMapping) EntityDiscriminatorMapping(org.hibernate.metamodel.mapping.EntityDiscriminatorMapping) PostInsertIdentifierGenerator(org.hibernate.id.PostInsertIdentifierGenerator) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) Assignable(org.hibernate.sql.ast.tree.update.Assignable) PostInsertIdentifierGenerator(org.hibernate.id.PostInsertIdentifierGenerator) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) BulkInsertionCapableIdentifierGenerator(org.hibernate.id.BulkInsertionCapableIdentifierGenerator) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Aggregations

BulkInsertionCapableIdentifierGenerator (org.hibernate.id.BulkInsertionCapableIdentifierGenerator)1 CompositeNestedGeneratedValueGenerator (org.hibernate.id.CompositeNestedGeneratedValueGenerator)1 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)1 PostInsertIdentifierGenerator (org.hibernate.id.PostInsertIdentifierGenerator)1 BasicEntityIdentifierMapping (org.hibernate.metamodel.mapping.BasicEntityIdentifierMapping)1 EntityDiscriminatorMapping (org.hibernate.metamodel.mapping.EntityDiscriminatorMapping)1 DiscriminatorSqmPath (org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)1 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)1 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)1 BasicValuedPathInterpretation (org.hibernate.query.sqm.sql.internal.BasicValuedPathInterpretation)1 SelfInterpretingSqmPath (org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath)1 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)1 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)1 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)1 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)1 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)1 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)1 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)1 Expression (org.hibernate.sql.ast.tree.expression.Expression)1 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)1