Search in sources :

Example 1 with MappingSubquery

use of com.blazebit.persistence.view.MappingSubquery in project blaze-persistence by Blazebit.

the class AnnotationMappingReader method getMapping.

public static Annotation getMapping(String attributeName, AnnotatedElement annotatedElement, boolean implicitMapping) {
    Mapping mapping = annotatedElement.getAnnotation(Mapping.class);
    if (mapping == null) {
        IdMapping idMapping = annotatedElement.getAnnotation(IdMapping.class);
        if (idMapping != null) {
            if (idMapping.value().isEmpty()) {
                idMapping = new IdMappingLiteral(attributeName);
            }
            return idMapping;
        }
        MappingParameter mappingParameter = annotatedElement.getAnnotation(MappingParameter.class);
        if (mappingParameter != null) {
            return mappingParameter;
        }
        MappingSubquery mappingSubquery = annotatedElement.getAnnotation(MappingSubquery.class);
        if (mappingSubquery != null) {
            return mappingSubquery;
        }
        MappingCorrelated mappingCorrelated = annotatedElement.getAnnotation(MappingCorrelated.class);
        if (mappingCorrelated != null) {
            return mappingCorrelated;
        }
        MappingCorrelatedSimple mappingCorrelatedSimple = annotatedElement.getAnnotation(MappingCorrelatedSimple.class);
        if (mappingCorrelatedSimple != null) {
            return mappingCorrelatedSimple;
        }
        if (implicitMapping) {
            mapping = new MappingLiteral(attributeName);
        } else {
            return null;
        }
    }
    if (mapping.value().isEmpty()) {
        mapping = new MappingLiteral(attributeName, mapping);
    }
    return mapping;
}
Also used : MappingCorrelatedSimple(com.blazebit.persistence.view.MappingCorrelatedSimple) MappingParameter(com.blazebit.persistence.view.MappingParameter) MappingCorrelated(com.blazebit.persistence.view.MappingCorrelated) EntityViewInheritanceMapping(com.blazebit.persistence.view.EntityViewInheritanceMapping) EntityViewRootMapping(com.blazebit.persistence.view.spi.EntityViewRootMapping) Mapping(com.blazebit.persistence.view.Mapping) IdMapping(com.blazebit.persistence.view.IdMapping) MappingSubquery(com.blazebit.persistence.view.MappingSubquery) IdMapping(com.blazebit.persistence.view.IdMapping)

Example 2 with MappingSubquery

use of com.blazebit.persistence.view.MappingSubquery in project blaze-persistence by Blazebit.

the class MetamodelBuildingContextImpl method getPossibleTargetTypes.

@Override
public List<ScalarTargetResolvingExpressionVisitor.TargetType> getPossibleTargetTypes(Class<?> entityClass, Attribute<?, ?> rootAttribute, Annotation mapping, Map<String, javax.persistence.metamodel.Type<?>> rootTypes) {
    ManagedType<?> managedType = entityMetamodel.getManagedType(entityClass);
    String expression;
    if (mapping instanceof Mapping) {
        expression = ((Mapping) mapping).value();
    } else if (mapping instanceof IdMapping) {
        expression = ((IdMapping) mapping).value();
    } else if (mapping instanceof MappingIndex) {
        expression = ((MappingIndex) mapping).value();
    } else if (mapping instanceof MappingCorrelatedSimple) {
        MappingCorrelatedSimple m = (MappingCorrelatedSimple) mapping;
        managedType = entityMetamodel.getManagedType(m.correlated());
        expression = m.correlationResult();
        // Correlation result is the correlated type, so the possible target type is the managed type
        if (expression.isEmpty()) {
            return Collections.<ScalarTargetResolvingExpressionVisitor.TargetType>singletonList(new ScalarTargetResolvingExpressionVisitor.TargetTypeImpl(false, null, managedType.getJavaType(), null, null));
        }
    } else if (mapping instanceof MappingCorrelated) {
        MappingCorrelated m = (MappingCorrelated) mapping;
        CorrelationProviderFactory correlationProviderFactory = CorrelationProviderHelper.getFactory(m.correlator());
        ScalarTargetResolvingExpressionVisitor resolver = new ScalarTargetResolvingExpressionVisitor(entityClass, getEntityMetamodel(), getJpqlFunctions(), rootTypes);
        javax.persistence.metamodel.Type<?> type = TypeExtractingCorrelationBuilder.extractType(correlationProviderFactory, "_alias", this, resolver);
        if (type == null) {
            // We can't determine the managed type
            return Collections.emptyList();
        }
        managedType = (ManagedType<?>) type;
        expression = m.correlationResult();
        // Correlation result is the correlated type, so the possible target type is the managed type
        if (expression.isEmpty()) {
            return Collections.<ScalarTargetResolvingExpressionVisitor.TargetType>singletonList(new ScalarTargetResolvingExpressionVisitor.TargetTypeImpl(false, null, managedType.getJavaType(), null, null));
        }
    } else if (mapping instanceof MappingSubquery) {
        MappingSubquery mappingSubquery = (MappingSubquery) mapping;
        if (!mappingSubquery.expression().isEmpty()) {
            Expression simpleExpression = typeValidationExpressionFactory.createSimpleExpression(((MappingSubquery) mapping).expression(), false, true, false);
            AliasReplacementVisitor aliasReplacementVisitor = new AliasReplacementVisitor(NullExpression.INSTANCE, mappingSubquery.subqueryAlias());
            simpleExpression.accept(aliasReplacementVisitor);
            ScalarTargetResolvingExpressionVisitor visitor = new ScalarTargetResolvingExpressionVisitor(entityClass, entityMetamodel, jpqlFunctions, rootTypes);
            simpleExpression.accept(visitor);
            return visitor.getPossibleTargetTypes();
        }
        // We can't determine the managed type
        return Collections.emptyList();
    } else {
        // There is no entity model type for other mappings
        return Collections.emptyList();
    }
    // Don't bother with empty expressions or missing managed type, let the validation process handle this
    if (expression.isEmpty() || managedType == null) {
        return Collections.emptyList();
    }
    expression = AbstractAttribute.stripThisFromMapping(expression);
    // The result is "THIS" apparently, so the possible target type is the managed type
    if (expression.isEmpty()) {
        Class<?> leafBaseClass = managedType.getJavaType();
        Class<?> leafBaseKeyClass = null;
        Class<?> leafBaseValueClass = null;
        if (rootAttribute instanceof PluralAttribute<?, ?, ?>) {
            leafBaseClass = rootAttribute.getJavaType();
            leafBaseValueClass = ((PluralAttribute<?, ?, ?>) rootAttribute).getElementType().getJavaType();
            if (rootAttribute instanceof MapAttribute<?, ?, ?>) {
                leafBaseKeyClass = ((MapAttribute<?, ?, ?>) rootAttribute).getKeyJavaType();
            }
        } else if (rootAttribute instanceof SingularAttribute<?, ?>) {
            leafBaseClass = rootAttribute.getJavaType();
        }
        return Collections.<ScalarTargetResolvingExpressionVisitor.TargetType>singletonList(new ScalarTargetResolvingExpressionVisitor.TargetTypeImpl(leafBaseValueClass != null, rootAttribute, leafBaseClass, leafBaseKeyClass, leafBaseValueClass));
    }
    Expression simpleExpression = typeValidationExpressionFactory.createSimpleExpression(expression, false, false, true);
    if (simpleExpression instanceof EntityLiteral) {
        // This is a special case where the mapping i.e. attribute name matches an entity name
        try {
            Attribute<?, ?> attribute = managedType.getAttribute(expression);
            return Collections.<ScalarTargetResolvingExpressionVisitor.TargetType>singletonList(new ScalarTargetResolvingExpressionVisitor.TargetTypeImpl(false, attribute, attribute.getJavaType(), null, attribute.getJavaType()));
        } catch (IllegalArgumentException ex) {
        // Apparently it's not an attribute, so let it run through
        }
    }
    ScalarTargetResolvingExpressionVisitor visitor = new ScalarTargetResolvingExpressionVisitor(managedType, rootAttribute, entityMetamodel, jpqlFunctions, rootTypes);
    simpleExpression.accept(visitor);
    return visitor.getPossibleTargetTypes();
}
Also used : EntityLiteral(com.blazebit.persistence.parser.expression.EntityLiteral) Mapping(com.blazebit.persistence.view.Mapping) IdMapping(com.blazebit.persistence.view.IdMapping) MappingSubquery(com.blazebit.persistence.view.MappingSubquery) IdMapping(com.blazebit.persistence.view.IdMapping) AliasReplacementVisitor(com.blazebit.persistence.parser.AliasReplacementVisitor) CorrelationProviderFactory(com.blazebit.persistence.view.CorrelationProviderFactory) MappingCorrelated(com.blazebit.persistence.view.MappingCorrelated) PluralAttribute(javax.persistence.metamodel.PluralAttribute) MappingIndex(com.blazebit.persistence.view.MappingIndex) ScalarTargetResolvingExpressionVisitor(com.blazebit.persistence.view.impl.ScalarTargetResolvingExpressionVisitor) MapAttribute(javax.persistence.metamodel.MapAttribute) MappingCorrelatedSimple(com.blazebit.persistence.view.MappingCorrelatedSimple) Expression(com.blazebit.persistence.parser.expression.Expression) NullExpression(com.blazebit.persistence.parser.expression.NullExpression)

Example 3 with MappingSubquery

use of com.blazebit.persistence.view.MappingSubquery in project blaze-persistence by Blazebit.

the class AnnotationMappingReader method getMapping.

public static Annotation getMapping(AnnotatedElement annotatedElement, Constructor<?> constructor, int index, MetamodelBootContext context) {
    if (annotatedElement.isAnnotationPresent(IdMapping.class)) {
        context.addError("The @IdMapping annotation is disallowed for entity view constructors for the " + ParameterAttributeMapping.getLocation(constructor, index));
        return null;
    }
    Mapping mapping = annotatedElement.getAnnotation(Mapping.class);
    if (mapping != null) {
        return mapping;
    }
    MappingParameter mappingParameter = annotatedElement.getAnnotation(MappingParameter.class);
    if (mappingParameter != null) {
        return mappingParameter;
    }
    MappingSubquery mappingSubquery = annotatedElement.getAnnotation(MappingSubquery.class);
    if (mappingSubquery != null) {
        return mappingSubquery;
    }
    MappingCorrelated mappingCorrelated = annotatedElement.getAnnotation(MappingCorrelated.class);
    if (mappingCorrelated != null) {
        return mappingCorrelated;
    }
    MappingCorrelatedSimple mappingCorrelatedSimple = annotatedElement.getAnnotation(MappingCorrelatedSimple.class);
    if (mappingCorrelatedSimple != null) {
        return mappingCorrelatedSimple;
    }
    Self self = annotatedElement.getAnnotation(Self.class);
    if (self != null) {
        return self;
    }
    context.addError("No entity view mapping annotation given for the " + ParameterAttributeMapping.getLocation(constructor, index));
    return null;
}
Also used : MappingCorrelatedSimple(com.blazebit.persistence.view.MappingCorrelatedSimple) MappingParameter(com.blazebit.persistence.view.MappingParameter) MappingCorrelated(com.blazebit.persistence.view.MappingCorrelated) EntityViewInheritanceMapping(com.blazebit.persistence.view.EntityViewInheritanceMapping) EntityViewRootMapping(com.blazebit.persistence.view.spi.EntityViewRootMapping) Mapping(com.blazebit.persistence.view.Mapping) IdMapping(com.blazebit.persistence.view.IdMapping) MappingSubquery(com.blazebit.persistence.view.MappingSubquery) Self(com.blazebit.persistence.view.Self)

Aggregations

IdMapping (com.blazebit.persistence.view.IdMapping)3 Mapping (com.blazebit.persistence.view.Mapping)3 MappingCorrelated (com.blazebit.persistence.view.MappingCorrelated)3 MappingCorrelatedSimple (com.blazebit.persistence.view.MappingCorrelatedSimple)3 MappingSubquery (com.blazebit.persistence.view.MappingSubquery)3 EntityViewInheritanceMapping (com.blazebit.persistence.view.EntityViewInheritanceMapping)2 MappingParameter (com.blazebit.persistence.view.MappingParameter)2 EntityViewRootMapping (com.blazebit.persistence.view.spi.EntityViewRootMapping)2 AliasReplacementVisitor (com.blazebit.persistence.parser.AliasReplacementVisitor)1 EntityLiteral (com.blazebit.persistence.parser.expression.EntityLiteral)1 Expression (com.blazebit.persistence.parser.expression.Expression)1 NullExpression (com.blazebit.persistence.parser.expression.NullExpression)1 CorrelationProviderFactory (com.blazebit.persistence.view.CorrelationProviderFactory)1 MappingIndex (com.blazebit.persistence.view.MappingIndex)1 Self (com.blazebit.persistence.view.Self)1 ScalarTargetResolvingExpressionVisitor (com.blazebit.persistence.view.impl.ScalarTargetResolvingExpressionVisitor)1 MapAttribute (javax.persistence.metamodel.MapAttribute)1 PluralAttribute (javax.persistence.metamodel.PluralAttribute)1