Skip to content

Commit

Permalink
Fix errors encountered within applications of curecomp
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Apr 24, 2017
1 parent ef28c5e commit 95c975f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,15 @@ public static Class<?> resolveFieldClass(Class<?> baseClass, Attribute<?, ?> att
}
}

return getConcreterClass(fieldClass, jpaReportedFieldClass);
if (fieldClass.isAssignableFrom(jpaReportedFieldClass)) {
return jpaReportedFieldClass;
} else if (jpaReportedFieldClass.isAssignableFrom(fieldClass)) {
return fieldClass;
} else {
// Hibernate reports the wrong type for fields that are differently bound via a type variable
// so we default in this erroneous case to the resolved java type instead of the jpa resolved type
return fieldClass;
}
}

public static AttributeHolder getAttributeForJoining(EntityMetamodel metamodel, PathExpression expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,25 +263,40 @@ public void visit(PropertyExpression expression) {
@Override
public void visit(GeneralCaseExpression expression) {
List<PathPosition> currentPositions = pathPositions;
List<PathPosition> newPositions = new ArrayList<PathPosition>();
List<PathPosition> newPositions = new ArrayList<>();

int positionsSize = currentPositions.size();
for (int j = 0; j < positionsSize; j++) {
List<WhenClauseExpression> expressions = expression.getWhenClauses();
int size = expressions.size();
for (int i = 0; i < size; i++) {
EXPRESSION_LOOP: for (int i = 0; i < size; i++) {
PathPosition position = currentPositions.get(j).copy();
pathPositions = new ArrayList<PathPosition>();
pathPositions = new ArrayList<>();
pathPositions.add(currentPosition = position);
expressions.get(i).accept(this);
newPositions.addAll(pathPositions);

// We just use the type of the first path position that we find
for (PathPosition newPosition : pathPositions) {
if (newPosition.getCurrentClass() != null) {
newPositions.add(newPosition);
break EXPRESSION_LOOP;
}
}
}

PathPosition position = currentPositions.get(j).copy();
pathPositions = new ArrayList<PathPosition>();
pathPositions.add(currentPosition = position);
expression.getDefaultExpr().accept(this);
newPositions.addAll(pathPositions);
if (newPositions.isEmpty()) {
PathPosition position = currentPositions.get(j).copy();
pathPositions = new ArrayList<>();
pathPositions.add(currentPosition = position);
expression.getDefaultExpr().accept(this);

// We just use the type of the first path position that we find
for (PathPosition newPosition : pathPositions) {
if (newPosition.getCurrentClass() != null) {
newPositions.add(newPosition);
}
}
}
}

currentPosition = null;
Expand Down Expand Up @@ -334,21 +349,30 @@ public void visit(ParameterExpression expression) {
// NOTE: We use null as marker for ANY TYPE
currentPosition.setCurrentClass(null);
} else {
// NOTE: parameters are only supported in the select clause when having an insert!
// If there are other branches(path positions) i.e. of a case when that have a type, we can allow parameters too
for (PathPosition position : pathPositions) {
if (position != currentPosition) {
if (position.getCurrentClass() != null) {
currentPosition.setCurrentClass(null);
return;
}
}
}
// NOTE: plain parameters are only supported in the select clause when having an insert!
invalid(expression, "Parameters are not allowed as results in mapping. Please use @MappingParameter for this instead!");
}
}

private void resolveToAny(List<Expression> expressions, boolean allowParams) {
private void resolveFirst(List<Expression> expressions, boolean allowParams) {
List<PathPosition> currentPositions = pathPositions;
List<PathPosition> newPositions = new ArrayList<PathPosition>();
List<PathPosition> newPositions = new ArrayList<>();

int positionsSize = currentPositions.size();
for (int j = 0; j < positionsSize; j++) {
int size = expressions.size();
for (int i = 0; i < size; i++) {
EXPRESSION_LOOP: for (int i = 0; i < size; i++) {
PathPosition position = currentPositions.get(j).copy();
pathPositions = new ArrayList<PathPosition>();
pathPositions = new ArrayList<>();
pathPositions.add(currentPosition = position);
if (allowParams) {
parametersAllowed = true;
Expand All @@ -357,7 +381,14 @@ private void resolveToAny(List<Expression> expressions, boolean allowParams) {
if (allowParams) {
parametersAllowed = false;
}
newPositions.addAll(pathPositions);

// We just use the type of the first path position that we find
for (PathPosition newPosition : pathPositions) {
if (newPosition.getCurrentClass() != null) {
newPositions.add(newPosition);
break EXPRESSION_LOOP;
}
}
}
}

Expand Down Expand Up @@ -484,15 +515,15 @@ public void visit(FunctionExpression expression) {
String name = expression.getFunctionName();
if ("FUNCTION".equalsIgnoreCase(name)) {
// Skip the function name
resolveToAny(expression.getExpressions().subList(1, expression.getExpressions().size()), true);
resolveToFunctionReturnType(expression.getExpressions().get(0).toString());
resolveFirst(expression.getExpressions().subList(1, expression.getExpressions().size()), true);
resolveToFunctionReturnType(((StringLiteral) expression.getExpressions().get(0)).getValue());
} else if (ExpressionUtils.isSizeFunction(expression)) {
// According to our grammar, we can only get a path here
PropertyExpression property = resolveBase((PathExpression) expression.getExpressions().get(0));
currentPosition.setMethod(resolve(currentPosition.getCurrentClass(), property.getProperty()));
currentPosition.setCurrentClass(Long.class);
} else {
resolveToAny(expression.getExpressions(), true);
resolveFirst(expression.getExpressions(), true);
resolveToFunctionReturnType(name);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ public static String replaceThisFromMapping(String mapping, String root) {
* @return The mappings which contain collection attribute uses
*/
public Set<String> getCollectionJoinMappings(ManagedType<?> managedType, MetamodelBuildingContext context) {
if (mapping == null || isQueryParameter()) {
// Subqueries and parameters can't be checked
if (mapping == null || isQueryParameter() || getAttributeType() == AttributeType.SINGULAR) {
// Subqueries and parameters can't be checked. When a collection is remapped to a singular attribute, we don't check it
return Collections.emptySet();
}

Expand Down Expand Up @@ -606,7 +606,7 @@ public void checkAttribute(ManagedType<?> managedType, MetamodelBuildingContext
// Validate that resolving "mapping" on "managedType" is compatible with "expressionType" and "elementType"
validateTypesCompatible(managedType, mapping, expressionType, elementType, subtypesAllowed, context, ExpressionLocation.MAPPING, getLocation());

if (isUpdatable()) {
if (isUpdatable() && declaringType.isUpdatable()) {
UpdatableExpressionVisitor visitor = new UpdatableExpressionVisitor(managedType.getJavaType());
try {
// NOTE: Not supporting "this" here because it doesn't make sense to have an updatable mapping that refers to this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.blazebit.persistence.view.Mapping;
import com.blazebit.persistence.view.MappingCorrelated;
import com.blazebit.persistence.view.MappingCorrelatedSimple;
import com.blazebit.persistence.view.MappingParameter;
import com.blazebit.persistence.view.MappingSubquery;
import com.blazebit.persistence.view.metamodel.MappingConstructor;
Expand Down Expand Up @@ -65,7 +66,8 @@ public static Annotation getMapping(Constructor<?> constructor, int index, Metam
if (MappingParameter.class.isInstance(a)
|| Mapping.class.isInstance(a)
|| MappingSubquery.class.isInstance(a)
|| MappingCorrelated.class.isInstance(a)) {
|| MappingCorrelated.class.isInstance(a)
|| MappingCorrelatedSimple.class.isInstance(a)) {
return a;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public boolean isIndexed() {
public abstract BatchFetch getBatchFetch();

public Class<?> getJavaType() {
return getType().getJavaType();
Type<?> t = getType();
if (t == null) {
return null;
}
return t.getJavaType();
}

public Type<?> getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public ManagedViewTypeImpl(ViewMapping viewMapping, MetamodelBuildingContext con

public void checkAttributes(MetamodelBuildingContext context) {
ManagedType<?> managedType = context.getEntityMetamodel().managedType(entityClass);
// Ensure that a plural entity attribute is not used multiple times in different plural entity view attributes
// If it were used multiple times, the second collection would not receive all expected elements, because both are based on the same join
// and the first collection will already cause a "fold" of the results for materializing the collection in the entity view
// We could theoretically try to defer the "fold" action, but the current model makes this pretty hard. The obvious workaround is to map a plural subview attribute
// and put all mappings into that. This will guarantee that the "fold" action only happens after all properties have been processed
Map<String, List<String>> collectionMappings = new HashMap<String, List<String>>();

for (AbstractMethodAttribute<? super X, ?> attribute : attributes.values()) {
Expand Down Expand Up @@ -174,6 +179,10 @@ public void checkNestedAttributes(List<AbstractAttribute<?, ?>> parents, Metamod

protected abstract boolean hasId();

public boolean isUpdatable() {
return false;
}

@Override
public Class<X> getJavaType() {
return javaType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ protected Class<?> resolveKeyType() {
Class<?> attributeType = ReflectionUtils.getResolvedMethodReturnType(entityViewClass, method);
Class<?>[] typeArguments = ReflectionUtils.getResolvedMethodReturnTypeArguments(entityViewClass, method);
// Force singular mapping
if (typeArguments.length == 0 || AnnotationUtils.findAnnotation(method, MappingSingular.class) != null || !Map.class.isAssignableFrom(attributeType)) {
if (typeArguments.length == 0 || AnnotationUtils.findAnnotation(method, MappingSingular.class) != null || AnnotationUtils.findAnnotation(method, MappingParameter.class) != null || !Map.class.isAssignableFrom(attributeType)) {
return null;
}

Expand All @@ -202,7 +202,7 @@ protected Class<?> resolveElementType() {
Class<?> attributeType = ReflectionUtils.getResolvedMethodReturnType(entityViewClass, method);
Class<?>[] typeArguments = ReflectionUtils.getResolvedMethodReturnTypeArguments(entityViewClass, method);
// Force singular mapping
if (typeArguments.length == 0 || AnnotationUtils.findAnnotation(method, MappingSingular.class) != null) {
if (typeArguments.length == 0 || AnnotationUtils.findAnnotation(method, MappingSingular.class) != null || AnnotationUtils.findAnnotation(method, MappingParameter.class) != null) {
return attributeType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ protected Class<?> resolveKeyType() {
Class<?>[] typeArguments = ReflectionUtils.resolveTypeArguments(concreteClass, parameterType);

// Force singular mapping
if (typeArguments.length == 0 || findAnnotation(MappingSingular.class) != null || !Map.class.isAssignableFrom(resolveType())) {
if (typeArguments.length == 0 || findAnnotation(MappingSingular.class) != null || findAnnotation(MappingParameter.class) != null || !Map.class.isAssignableFrom(resolveType())) {
return null;
}

Expand All @@ -196,7 +196,7 @@ protected Class<?> resolveElementType() {
Type parameterType = constructor.getGenericParameterTypes()[index];
Class<?>[] typeArguments = ReflectionUtils.resolveTypeArguments(concreteClass, parameterType);
// Force singular mapping
if (typeArguments.length == 0 || findAnnotation(MappingSingular.class) != null) {
if (typeArguments.length == 0 || findAnnotation(MappingSingular.class) != null || findAnnotation(MappingParameter.class) != null) {
return resolveType();
}

Expand Down

0 comments on commit 95c975f

Please sign in to comment.