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