Search in sources :

Example 1 with PathTargetResolvingExpressionVisitor

use of com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor in project blaze-persistence by Blazebit.

the class JpaUtils method getAttributeForJoining.

public static AttributeHolder getAttributeForJoining(EntityMetamodel metamodel, Type<?> baseNodeType, Expression joinExpression, String baseNodeAlias) {
    PathTargetResolvingExpressionVisitor visitor = new PathTargetResolvingExpressionVisitor(metamodel, baseNodeType, baseNodeAlias);
    joinExpression.accept(visitor);
    Map<Attribute<?, ?>, Type<?>> possibleTargets = visitor.getPossibleTargets();
    if (possibleTargets.size() > 1) {
        throw new IllegalArgumentException("Multiple possible target types for expression: " + joinExpression);
    }
    Map.Entry<Attribute<?, ?>, Type<?>> entry = possibleTargets.entrySet().iterator().next();
    return new AttributeHolder(entry.getKey(), entry.getValue());
}
Also used : PathTargetResolvingExpressionVisitor(com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor) EntityType(javax.persistence.metamodel.EntityType) DbmsStatementType(com.blazebit.persistence.spi.DbmsStatementType) Type(javax.persistence.metamodel.Type) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) ManagedType(javax.persistence.metamodel.ManagedType) MapAttribute(javax.persistence.metamodel.MapAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) ListAttribute(javax.persistence.metamodel.ListAttribute) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with PathTargetResolvingExpressionVisitor

use of com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor in project blaze-persistence by Blazebit.

the class MetamodelUtils method isIndexedList.

public static boolean isIndexedList(EntityMetamodel metamodel, Class<?> entityClass, Expression expression, String mapping) {
    PathTargetResolvingExpressionVisitor visitor = new PathTargetResolvingExpressionVisitor(metamodel, metamodel.managedType(entityClass), null);
    expression.accept(visitor);
    Map<Attribute<?, ?>, javax.persistence.metamodel.Type<?>> possibleTargets = visitor.getPossibleTargets();
    Iterator<Map.Entry<Attribute<?, ?>, javax.persistence.metamodel.Type<?>>> iter = possibleTargets.entrySet().iterator();
    // It must have one, otherwise a parse error would have been thrown already
    Map.Entry<Attribute<?, ?>, ?> targetEntry = iter.next();
    boolean indexed = isIndexedList(targetEntry.getKey());
    while (iter.hasNext()) {
        targetEntry = iter.next();
        if (indexed != isIndexedList(targetEntry.getKey())) {
            throw new IllegalArgumentException("Inconclusive result on checking whether the expression [" + mapping + "] resolves to an indexed list on class [" + entityClass.getName() + "].");
        }
    }
    return indexed;
}
Also used : PathTargetResolvingExpressionVisitor(com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Type(java.lang.reflect.Type) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 3 with PathTargetResolvingExpressionVisitor

use of com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor in project blaze-persistence by Blazebit.

the class AbstractCommonQueryBuilder method joinOnForLateralEmulation.

private JoinOnBuilder<BuilderType> joinOnForLateralEmulation(String correlationPath, String alias, JoinType type) {
    Expression expr = expressionFactory.createJoinPathExpression(correlationPath);
    JoinNode baseNode = null;
    String baseNodeAlias = null;
    if (expr instanceof PathExpression) {
        PathElementExpression firstElem = ExpressionUtils.getLeftMostPathExpression((PathExpression) expr).getExpressions().get(0);
        AliasInfo startAlias;
        if (firstElem instanceof ArrayExpression) {
            startAlias = null;
        } else if (firstElem instanceof PropertyExpression) {
            startAlias = aliasManager.getAliasInfo(((PropertyExpression) firstElem).getProperty());
        } else {
            throw new IllegalArgumentException("Unexpected expression type[" + firstElem.getClass().getSimpleName() + "] in expression: " + correlationPath);
        }
        if (startAlias instanceof JoinAliasInfo) {
            baseNodeAlias = startAlias.getAlias();
            baseNode = ((JoinAliasInfo) startAlias).getJoinNode();
        }
    } else {
        throw new IllegalArgumentException("Unexpected expression type[" + expr.getClass().getSimpleName() + "] in expression: " + correlationPath);
    }
    if (baseNode == null) {
        baseNode = getRoot();
    }
    PathTargetResolvingExpressionVisitor visitor = new PathTargetResolvingExpressionVisitor(getMetamodel(), baseNode.getType(), baseNodeAlias);
    expr.accept(visitor);
    Map<Attribute<?, ?>, Type<?>> possibleTargets = visitor.getPossibleTargets();
    Type<?> t;
    if (possibleTargets.size() == 1 && (t = possibleTargets.values().iterator().next()) instanceof EntityType<?>) {
        return joinOn((EntityType<?>) t, alias, type);
    }
    return joinOn(expr, alias, type);
}
Also used : PathTargetResolvingExpressionVisitor(com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor) PluralAttribute(javax.persistence.metamodel.PluralAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) NumericType(com.blazebit.persistence.parser.expression.NumericType) SetOperationType(com.blazebit.persistence.spi.SetOperationType) DbmsStatementType(com.blazebit.persistence.spi.DbmsStatementType) ManagedType(javax.persistence.metamodel.ManagedType) Type(javax.persistence.metamodel.Type) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) IdentifiableType(javax.persistence.metamodel.IdentifiableType) JoinType(com.blazebit.persistence.JoinType) EntityType(javax.persistence.metamodel.EntityType) TemporalType(javax.persistence.TemporalType) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) ParameterExpression(javax.persistence.criteria.ParameterExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) Expression(com.blazebit.persistence.parser.expression.Expression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression)

Example 4 with PathTargetResolvingExpressionVisitor

use of com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor in project blaze-persistence by Blazebit.

the class AbstractMethodAttribute method checkAttribute.

@Override
public void checkAttribute(ManagedType<?> managedType, MetamodelBuildingContext context) {
    super.checkAttribute(managedType, context);
    if (isUpdatable() && declaringType.isUpdatable()) {
        String mappedBy = getMappedBy();
        if (mappedBy != null && getInverseRemoveStrategy() == InverseRemoveStrategy.SET_NULL) {
            Type<?> elementType = getElementType();
            ManagedType<?> elementJpaType;
            if (elementType instanceof ManagedViewTypeImplementor<?>) {
                elementJpaType = ((ManagedViewTypeImplementor<?>) elementType).getJpaManagedType();
            } else {
                elementJpaType = ((BasicTypeImpl<?>) elementType).getManagedType();
            }
            Map<String, String> writableMappedByMappings = getWritableMappedByMappings();
            if (writableMappedByMappings == null) {
                Attribute<?, ?> attribute = elementJpaType.getAttribute(mappedBy);
                // So we just care about singular attributes here
                if (attribute instanceof SingularAttribute<?, ?>) {
                    if (!((SingularAttribute<?, ?>) attribute).isOptional()) {
                        context.addError("Illegal use of the remove strategy SET_NULL for non-nullable mapped by attribute '" + mappedBy + "' at " + getLocation() + " Use a different strategy via @MappingInverse(removeStrategy = InverseRemoveStrategy...)");
                    }
                }
            } else {
                PathTargetResolvingExpressionVisitor visitor = new PathTargetResolvingExpressionVisitor(context.getEntityMetamodel(), elementJpaType, null);
                for (String value : writableMappedByMappings.values()) {
                    visitor.clear();
                    context.getTypeValidationExpressionFactory().createPathExpression(value).accept(visitor);
                    Map<Attribute<?, ?>, javax.persistence.metamodel.Type<?>> possibleTargets = visitor.getPossibleTargets();
                    if (possibleTargets.size() > 1) {
                        context.addError("Multiple possible target type for the mapping in the " + getLocation() + ": " + possibleTargets);
                    }
                    Attribute<?, ?> attribute = possibleTargets.keySet().iterator().next();
                    if (attribute instanceof SingularAttribute<?, ?>) {
                        if (!((SingularAttribute<?, ?>) attribute).isOptional()) {
                            context.addError("Illegal use of the remove strategy SET_NULL for non-nullable mapped by attribute '" + mappedBy + "' because writable mapping '" + value + "' is non-optional at " + getLocation() + " Use a different strategy via @MappingInverse(removeStrategy = InverseRemoveStrategy...)");
                        }
                    }
                }
            }
        }
    }
}
Also used : PathTargetResolvingExpressionVisitor(com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor) SingularAttribute(javax.persistence.metamodel.SingularAttribute) BasicType(com.blazebit.persistence.view.metamodel.BasicType) Type(com.blazebit.persistence.view.metamodel.Type) ManagedType(javax.persistence.metamodel.ManagedType) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) MethodAttribute(com.blazebit.persistence.view.metamodel.MethodAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute)

Example 5 with PathTargetResolvingExpressionVisitor

use of com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor in project blaze-persistence by Blazebit.

the class JoinManager method findCorrelatedAttributeIndex.

private int findCorrelatedAttributeIndex(JoinNode correlationParent, List<PathElementExpression> pathElements, int start, int end) {
    PathTargetResolvingExpressionVisitor pathResolvingVisitor = new PathTargetResolvingExpressionVisitor(metamodel, correlationParent.getNodeType(), correlationParent.getAlias());
    for (int i = start; i < end; i++) {
        PathElementExpression expression = pathElements.get(i);
        expression.accept(pathResolvingVisitor);
        Attribute<?, ?> attribute = pathResolvingVisitor.getPossibleTargets().entrySet().iterator().next().getKey();
        if (attribute != null) {
            if (mainQuery.jpaProvider.getJpaMetamodelAccessor().isJoinable(attribute)) {
                return i;
            }
        }
    }
    return -1;
}
Also used : PathTargetResolvingExpressionVisitor(com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression)

Aggregations

PathTargetResolvingExpressionVisitor (com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor)5 Attribute (javax.persistence.metamodel.Attribute)4 ManagedType (javax.persistence.metamodel.ManagedType)3 PathElementExpression (com.blazebit.persistence.parser.expression.PathElementExpression)2 DbmsStatementType (com.blazebit.persistence.spi.DbmsStatementType)2 ExtendedAttribute (com.blazebit.persistence.spi.ExtendedAttribute)2 ExtendedManagedType (com.blazebit.persistence.spi.ExtendedManagedType)2 Map (java.util.Map)2 EntityType (javax.persistence.metamodel.EntityType)2 ListAttribute (javax.persistence.metamodel.ListAttribute)2 MapAttribute (javax.persistence.metamodel.MapAttribute)2 PluralAttribute (javax.persistence.metamodel.PluralAttribute)2 SingularAttribute (javax.persistence.metamodel.SingularAttribute)2 Type (javax.persistence.metamodel.Type)2 JoinType (com.blazebit.persistence.JoinType)1 ArrayExpression (com.blazebit.persistence.parser.expression.ArrayExpression)1 Expression (com.blazebit.persistence.parser.expression.Expression)1 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)1 NumericType (com.blazebit.persistence.parser.expression.NumericType)1 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)1