Search in sources :

Example 1 with NegatedPredicate

use of org.hibernate.sql.ast.tree.predicate.NegatedPredicate in project hibernate-orm by hibernate.

the class SingleTableEntityPersister method createDiscriminatorPredicate.

private Predicate createDiscriminatorPredicate(String alias, TableGroup tableGroup, SqlExpressionResolver sqlExpressionResolver) {
    final String columnReferenceKey;
    final String discriminatorExpression;
    if (isDiscriminatorFormula()) {
        discriminatorExpression = getDiscriminatorFormulaTemplate();
        columnReferenceKey = SqlExpressionResolver.createColumnReferenceKey(tableGroup.getPrimaryTableReference(), getDiscriminatorFormulaTemplate());
    } else {
        discriminatorExpression = getDiscriminatorColumnName();
        columnReferenceKey = SqlExpressionResolver.createColumnReferenceKey(tableGroup.getPrimaryTableReference(), getDiscriminatorColumnName());
    }
    final BasicType<?> discriminatorType = (BasicType<?>) getDiscriminatorType();
    final Expression sqlExpression = sqlExpressionResolver.resolveSqlExpression(columnReferenceKey, sqlAstProcessingState -> new ColumnReference(alias, discriminatorExpression, isDiscriminatorFormula(), null, null, discriminatorType.getJdbcMapping(), getFactory()));
    if (hasSubclasses()) {
        final List<Expression> values = new ArrayList<>(fullDiscriminatorValues.length);
        boolean hasNull = false, hasNonNull = false;
        for (Object discriminatorValue : fullDiscriminatorValues) {
            if (discriminatorValue == DiscriminatorHelper.NULL_DISCRIMINATOR) {
                hasNull = true;
            } else if (discriminatorValue == DiscriminatorHelper.NOT_NULL_DISCRIMINATOR) {
                hasNonNull = true;
            } else {
                values.add(new QueryLiteral<>(discriminatorValue, discriminatorType));
            }
        }
        final Predicate p = new InListPredicate(sqlExpression, values);
        if (hasNull || hasNonNull) {
            final Junction junction = new Junction(Junction.Nature.DISJUNCTION);
            // so we return an empty Junction
            if (hasNull && hasNonNull) {
                return junction;
            }
            junction.add(new NullnessPredicate(sqlExpression));
            junction.add(p);
            return junction;
        }
        return p;
    }
    final Object value = getDiscriminatorValue();
    final boolean hasNotNullDiscriminator = value == DiscriminatorHelper.NOT_NULL_DISCRIMINATOR;
    final boolean hasNullDiscriminator = value == DiscriminatorHelper.NULL_DISCRIMINATOR;
    if (hasNotNullDiscriminator || hasNullDiscriminator) {
        final NullnessPredicate nullnessPredicate = new NullnessPredicate(sqlExpression);
        if (hasNotNullDiscriminator) {
            return new NegatedPredicate(nullnessPredicate);
        }
        return nullnessPredicate;
    }
    return new ComparisonPredicate(sqlExpression, ComparisonOperator.EQUAL, new QueryLiteral<>(value, discriminatorType));
}
Also used : BasicType(org.hibernate.type.BasicType) ArrayList(java.util.ArrayList) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) Expression(org.hibernate.sql.ast.tree.expression.Expression) Junction(org.hibernate.sql.ast.tree.predicate.Junction) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Aggregations

ArrayList (java.util.ArrayList)1 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)1 Expression (org.hibernate.sql.ast.tree.expression.Expression)1 QueryLiteral (org.hibernate.sql.ast.tree.expression.QueryLiteral)1 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)1 InListPredicate (org.hibernate.sql.ast.tree.predicate.InListPredicate)1 Junction (org.hibernate.sql.ast.tree.predicate.Junction)1 NegatedPredicate (org.hibernate.sql.ast.tree.predicate.NegatedPredicate)1 NullnessPredicate (org.hibernate.sql.ast.tree.predicate.NullnessPredicate)1 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)1 BasicType (org.hibernate.type.BasicType)1