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;
}
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();
}
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;
}
Aggregations