Search in sources :

Example 1 with VersionTypeSeedParameterSpecification

use of org.hibernate.sql.exec.internal.VersionTypeSeedParameterSpecification 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)

Example 2 with VersionTypeSeedParameterSpecification

use of org.hibernate.sql.exec.internal.VersionTypeSeedParameterSpecification in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method addVersionedAssignment.

public void addVersionedAssignment(Consumer<Assignment> assignmentConsumer, SqmUpdateStatement<?> sqmStatement) {
    if (!sqmStatement.isVersioned()) {
        return;
    }
    final EntityPersister persister = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor(sqmStatement.getTarget().getEntityName());
    if (!persister.isVersioned()) {
        throw new SemanticException("increment option specified for update of non-versioned entity");
    }
    final BasicType<?> versionType = persister.getVersionType();
    if (versionType instanceof UserVersionType) {
        throw new SemanticException("user-defined version types not supported for increment option");
    }
    final EntityVersionMapping versionMapping = persister.getVersionMapping();
    final List<ColumnReference> targetColumnReferences = BasicValuedPathInterpretation.from((SqmBasicValuedSimplePath<?>) sqmStatement.getRoot().get(versionMapping.getPartName()), this, this, jpaQueryComplianceEnabled).getColumnReferences();
    assert targetColumnReferences.size() == 1;
    final ColumnReference versionColumn = targetColumnReferences.get(0);
    final Expression value;
    if (versionMapping.getJdbcMapping().getJdbcType().isTemporal()) {
        value = new VersionTypeSeedParameterSpecification(versionType, persister.getVersionJavaType());
    } else {
        value = new BinaryArithmeticExpression(versionColumn, ADD, new QueryLiteral<>(1, versionType), versionType);
    }
    assignmentConsumer.accept(new Assignment(versionColumn, value));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SingleTableEntityPersister(org.hibernate.persister.entity.SingleTableEntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) UserVersionType(org.hibernate.usertype.UserVersionType) VersionTypeSeedParameterSpecification(org.hibernate.sql.exec.internal.VersionTypeSeedParameterSpecification) SqmBasicValuedSimplePath(org.hibernate.query.sqm.tree.domain.SqmBasicValuedSimplePath) Assignment(org.hibernate.sql.ast.tree.update.Assignment) SqmAssignment(org.hibernate.query.sqm.tree.update.SqmAssignment) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) ConvertedQueryLiteral(org.hibernate.sql.ast.tree.expression.ConvertedQueryLiteral) 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) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) EntityVersionMapping(org.hibernate.metamodel.mapping.EntityVersionMapping) SemanticException(org.hibernate.query.SemanticException) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Aggregations

SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)2 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)2 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)2 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)2 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)2 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)2 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)2 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)2 Expression (org.hibernate.sql.ast.tree.expression.Expression)2 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)2 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)2 SelfRenderingSqlFragmentExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression)2 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)2 VersionTypeSeedParameterSpecification (org.hibernate.sql.exec.internal.VersionTypeSeedParameterSpecification)2 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