use of com.blazebit.persistence.parser.expression.PathExpression in project blaze-persistence by Blazebit.
the class OrderByManager method applyFrom.
String[] applyFrom(OrderByManager orderByManager, Map<String, Integer> identifierExpressionStringMap) {
String[] identifierToUseSelectAliases = new String[identifierExpressionStringMap.size()];
for (int i = 0; i < orderByManager.orderByInfos.size(); i++) {
OrderByInfo info = orderByManager.orderByInfos.get(i);
String potentialSelectAlias = info.getExpressionString();
AliasInfo aliasInfo = orderByManager.aliasManager.getAliasInfo(potentialSelectAlias);
Expression expression;
if (aliasInfo instanceof SelectInfo) {
SelectInfo selectInfo = (SelectInfo) aliasInfo;
Integer selectItemIndex = identifierExpressionStringMap.get(selectInfo.getExpression().toString());
if (selectItemIndex != null) {
// We need to use the same alias as in the SQL because Hibernate for some reason does not resolve aliases in the order by clause of subqueries
String alias = ColumnTruncFunction.SYNTHETIC_COLUMN_PREFIX + selectItemIndex;
identifierToUseSelectAliases[selectItemIndex] = alias;
expression = new PathExpression(new PropertyExpression(alias));
} else {
// We have an order by item with an alias that is not part of the identifier expression map
Expression copiedSelectExpression = selectInfo.getExpression().copy(ExpressionCopyContext.EMPTY);
if (selectInfo.getExpression() instanceof PathExpression) {
expression = copiedSelectExpression;
} else {
String alias = aliasManager.generateRootAlias("_generated_alias");
selectManager.select(copiedSelectExpression, alias);
List<Expression> args = new ArrayList<>(2);
args.add(new PathExpression(new PropertyExpression(alias)));
args.add(new StringLiteral(alias));
expression = new FunctionExpression(AliasFunction.FUNCTION_NAME, args);
}
}
} else {
expression = info.getExpression().copy(ExpressionCopyContext.EMPTY);
}
orderBy(subqueryInitFactory.reattachSubqueries(expression, ClauseType.ORDER_BY), info.ascending, info.nullFirst);
}
return identifierToUseSelectAliases;
}
use of com.blazebit.persistence.parser.expression.PathExpression in project blaze-persistence by Blazebit.
the class JoinVisitor method removeAssociationIdIfPossible.
private void removeAssociationIdIfPossible(Expression left, Expression right) {
if (needsSingleValuedAssociationIdRemoval && fromClause == ClauseType.JOIN) {
if (removeAssociationIdIfPossible(left)) {
// The left expression was successfully rewritten
if (!removeAssociationIdIfPossible(right)) {
// But the right expression failed
Type<?> associationType = getAssociationType(left, right);
ParameterValueTransformer tranformer = parameterTransformerFactory.getToEntityTranformer(associationType.getJavaType());
if (!rewriteToAssociationParam(tranformer, right)) {
// If the other part wasn't a parameter, we have to do a "normal" implicit join
left.accept(this);
right.accept(this);
}
}
} else {
if (removeAssociationIdIfPossible(right)) {
// The right expression was successfully rewritten, but not the left
Type<?> associationType = getAssociationType(left, right);
ParameterValueTransformer tranformer = parameterTransformerFactory.getToEntityTranformer(associationType.getJavaType());
if (!rewriteToAssociationParam(tranformer, left)) {
// If the other part wasn't a parameter, we have to do a "normal" implicit join
left.accept(this);
right.accept(this);
}
} else {
left.accept(this);
right.accept(this);
}
}
} else {
String naturalIdAttribute;
if (fromClause == ClauseType.JOIN && left instanceof PathExpression && right instanceof PathExpression && (naturalIdAttribute = getNaturalIdAttribute(left, right)) != null) {
// We can only fix this if both expressions are path expressions
((PathExpression) left).getExpressions().add(new PropertyExpression(naturalIdAttribute));
((PathExpression) right).getExpressions().add(new PropertyExpression(naturalIdAttribute));
// Re-visit to update path reference
left.accept(this);
right.accept(this);
} else {
left.accept(this);
right.accept(this);
}
}
}
use of com.blazebit.persistence.parser.expression.PathExpression in project blaze-persistence by Blazebit.
the class JpaUtils method getAttributeForJoining.
public static AttributeHolder getAttributeForJoining(EntityMetamodel metamodel, PathExpression expression) {
JoinNode expressionBaseNode = ((JoinNode) expression.getPathReference().getBaseNode());
Expression p = expression.getExpressions().get(0);
while (!(p instanceof PropertyExpression)) {
if (p instanceof PathExpression) {
p = ((PathExpression) p).getExpressions().get(0);
} else if (p instanceof QualifiedExpression) {
p = ((QualifiedExpression) p).getPath().getExpressions().get(0);
} else if (p instanceof ArrayExpression) {
p = ((ArrayExpression) p).getBase();
} else {
p = ((TreatExpression) p).getExpression();
}
}
String firstElementString = p.toString();
String baseNodeAlias;
JoinNode baseNode = expressionBaseNode;
do {
baseNodeAlias = baseNode.getAlias();
} while (!firstElementString.equals(baseNodeAlias) && (baseNode = baseNode.getParent()) != null);
if (baseNode == null) {
baseNodeAlias = null;
if (expressionBaseNode.getParent() == null) {
baseNode = expressionBaseNode;
} else {
baseNode = expressionBaseNode.getParent();
}
}
return getAttributeForJoining(metamodel, baseNode.getNodeType(), expression, baseNodeAlias);
}
use of com.blazebit.persistence.parser.expression.PathExpression in project blaze-persistence by Blazebit.
the class JpaUtils method expandBindings.
public static void expandBindings(Map<String, Integer> bindingMap, Map<String, String> columnBindingMap, Map<String, ExtendedAttribute<?, ?>> attributeEntries, ClauseType clause, AbstractCommonQueryBuilder<?, ?, ?, ?, ?> queryBuilder, String keyFunctionExpression, boolean enableElementCollectionIdCutoff) {
SelectManager<?> selectManager = queryBuilder.selectManager;
JoinManager joinManager = queryBuilder.joinManager;
ParameterManager parameterManager = queryBuilder.parameterManager;
JpaProvider jpaProvider = queryBuilder.mainQuery.jpaProvider;
EntityMetamodelImpl metamodel = queryBuilder.mainQuery.metamodel;
boolean requiresNullCast = queryBuilder.mainQuery.dbmsDialect.requiresNullCast();
boolean needsCastParameters = queryBuilder.mainQuery.dbmsDialect.needsCastParameters();
JpaMetamodelAccessor jpaMetamodelAccessor = jpaProvider.getJpaMetamodelAccessor();
boolean needsElementCollectionIdCutoff = enableElementCollectionIdCutoff && jpaProvider.needsElementCollectionIdCutoff();
final Queue<String> attributeQueue = new ArrayDeque<>(bindingMap.keySet());
while (!attributeQueue.isEmpty()) {
final String attributeName = attributeQueue.remove();
Integer tupleIndex = bindingMap.get(attributeName);
Class<?> elementType;
String columnType;
boolean splitExpression;
ExtendedAttribute<?, ?> attributeEntry = attributeEntries.get(attributeName);
if (attributeEntry == null) {
if (!attributeName.equalsIgnoreCase(keyFunctionExpression)) {
continue;
}
String realAttributeName = attributeName.substring(attributeName.indexOf('(') + 1, attributeName.length() - 1);
attributeEntry = attributeEntries.get(realAttributeName);
if (attributeEntry.getAttribute() instanceof ListAttribute<?, ?>) {
elementType = Integer.class;
columnType = queryBuilder.mainQuery.dbmsDialect.getSqlType(Integer.class);
} else {
MapAttribute<?, ?, ?> mapAttribute = (MapAttribute<?, ?, ?>) attributeEntry.getAttribute();
elementType = mapAttribute.getKeyJavaType();
columnType = attributeEntry.getJoinTable() != null && attributeEntry.getJoinTable().getKeyColumnTypes() != null && attributeEntry.getJoinTable().getKeyColumnTypes().size() == 1 ? attributeEntry.getJoinTable().getKeyColumnTypes().values().iterator().next() : null;
}
splitExpression = false;
} else {
elementType = attributeEntry.getElementClass();
columnType = attributeEntry.getColumnTypes().length == 0 ? null : attributeEntry.getColumnTypes()[0];
final List<Attribute<?, ?>> attributePath = attributeEntry.getAttributePath();
final Attribute<?, ?> lastAttribute = attributePath.get(attributePath.size() - 1);
splitExpression = lastAttribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED;
if (!splitExpression) {
if ((clause != ClauseType.SET || jpaProvider.supportsUpdateSetAssociationId()) && jpaMetamodelAccessor.isJoinable(lastAttribute) && !isBasicElementType(lastAttribute)) {
splitExpression = true;
if (needsElementCollectionIdCutoff) {
OUTER: for (int i = 0; i < attributePath.size() - 1; i++) {
Attribute<?, ?> attribute = attributePath.get(i);
if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ELEMENT_COLLECTION) {
// This is a special case, when an embeddable is between an element collection and the association, we still need to split the expression
for (int j = i + 1; j < attributePath.size() - 1; j++) {
attribute = attributePath.get(j);
if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED) {
break OUTER;
}
}
splitExpression = false;
break;
}
}
}
}
}
}
SelectInfo selectInfo = selectManager.getSelectInfos().get(tupleIndex);
final Expression selectExpression = selectInfo.getExpression();
if (splitExpression) {
// TODO: Maybe also allow Treat, Case-When, Array?
if (selectExpression instanceof NullExpression) {
final Collection<String> embeddedPropertyNames = getEmbeddedPropertyPaths(attributeEntries, attributeName, needsElementCollectionIdCutoff, false);
if (embeddedPropertyNames.size() > 0) {
selectManager.getSelectInfos().remove(tupleIndex.intValue());
bindingMap.remove(attributeName);
// We are going to insert the expanded attributes as new select items and shift existing ones
int delta = embeddedPropertyNames.size() - 1;
if (delta > 0) {
for (Map.Entry<String, Integer> entry : bindingMap.entrySet()) {
if (entry.getValue() > tupleIndex) {
entry.setValue(entry.getValue() + delta);
}
}
}
int offset = 0;
for (String embeddedPropertyName : embeddedPropertyNames) {
String nestedAttributePath = attributeName + "." + embeddedPropertyName;
ExtendedAttribute<?, ?> nestedAttributeEntry = attributeEntries.get(nestedAttributePath);
// Process the nested attribute path recursively
attributeQueue.add(nestedAttributePath);
// Replace this binding in the binding map, additional selects need an updated index
bindingMap.put(nestedAttributePath, tupleIndex + offset);
selectManager.select(offset == 0 ? selectExpression : selectExpression.copy(ExpressionCopyContext.EMPTY), null, tupleIndex + offset);
if (columnBindingMap != null) {
for (String column : nestedAttributeEntry.getColumnNames()) {
columnBindingMap.put(column, nestedAttributePath);
}
}
offset++;
}
}
} else if (selectExpression instanceof PathExpression) {
boolean firstBinding = true;
final Collection<String> embeddedPropertyNames = getEmbeddedPropertyPaths(attributeEntries, attributeName, needsElementCollectionIdCutoff, false);
PathExpression baseExpression = embeddedPropertyNames.size() > 1 ? ((PathExpression) selectExpression).copy(ExpressionCopyContext.EMPTY) : ((PathExpression) selectExpression);
joinManager.implicitJoin(baseExpression, true, true, true, null, ClauseType.SELECT, new HashSet<String>(), false, false, false, false);
if (elementType != baseExpression.getPathReference().getType().getJavaType()) {
throw new IllegalStateException("An association should be bound to its association type and not its identifier type");
}
if (embeddedPropertyNames.size() > 0) {
bindingMap.remove(attributeName);
// We are going to insert the expanded attributes as new select items and shift existing ones
int delta = embeddedPropertyNames.size() - 1;
if (delta > 0) {
for (Map.Entry<String, Integer> entry : bindingMap.entrySet()) {
if (entry.getValue() > tupleIndex) {
entry.setValue(entry.getValue() + delta);
}
}
}
int offset = 0;
for (String embeddedPropertyName : embeddedPropertyNames) {
PathExpression pathExpression = firstBinding ? ((PathExpression) selectExpression) : baseExpression.copy(ExpressionCopyContext.EMPTY);
for (String propertyNamePart : embeddedPropertyName.split("\\.")) {
pathExpression.getExpressions().add(new PropertyExpression(propertyNamePart));
}
String nestedAttributePath = attributeName + "." + embeddedPropertyName;
ExtendedAttribute<?, ?> nestedAttributeEntry = attributeEntries.get(nestedAttributePath);
// Process the nested attribute path recursively
attributeQueue.add(nestedAttributePath);
// Replace this binding in the binding map, additional selects need an updated index
bindingMap.put(nestedAttributePath, firstBinding ? tupleIndex : tupleIndex + offset);
if (!firstBinding) {
selectManager.select(pathExpression, null, tupleIndex + offset);
} else {
firstBinding = false;
}
if (columnBindingMap != null) {
for (String column : nestedAttributeEntry.getColumnNames()) {
columnBindingMap.put(column, nestedAttributePath);
}
}
offset++;
}
}
} else if (selectExpression instanceof ParameterExpression) {
final Collection<String> embeddedPropertyNames = getEmbeddedPropertyPaths(attributeEntries, attributeName, jpaProvider.needsElementCollectionIdCutoff(), false);
if (embeddedPropertyNames.size() > 0) {
ParameterExpression parameterExpression = (ParameterExpression) selectExpression;
String parameterName = parameterExpression.getName();
Map<String, List<String>> parameterAccessPaths = new HashMap<>(embeddedPropertyNames.size());
ParameterValueTransformer tranformer = parameterManager.getParameter(parameterName).getTransformer();
if (tranformer instanceof SplittingParameterTransformer) {
for (String name : ((SplittingParameterTransformer) tranformer).getParameterNames()) {
parameterManager.unregisterParameterName(name, clause, queryBuilder);
}
}
selectManager.getSelectInfos().remove(tupleIndex.intValue());
bindingMap.remove(attributeName);
// We are going to insert the expanded attributes as new select items and shift existing ones
int delta = embeddedPropertyNames.size() - 1;
if (delta > 0) {
for (Map.Entry<String, Integer> entry : bindingMap.entrySet()) {
if (entry.getValue() > tupleIndex) {
entry.setValue(entry.getValue() + delta);
}
}
}
int offset = 0;
for (String embeddedPropertyName : embeddedPropertyNames) {
String subParamName = "_" + parameterName + "_" + embeddedPropertyName.replace('.', '_');
parameterManager.registerParameterName(subParamName, false, clause, queryBuilder);
parameterAccessPaths.put(subParamName, Arrays.asList(embeddedPropertyName.split("\\.")));
String nestedAttributePath = attributeName + "." + embeddedPropertyName;
ExtendedAttribute<?, ?> nestedAttributeEntry = attributeEntries.get(nestedAttributePath);
// Process the nested attribute path recursively
attributeQueue.add(nestedAttributePath);
// Replace this binding in the binding map, additional selects need an updated index
bindingMap.put(nestedAttributePath, tupleIndex + offset);
selectManager.select(new ParameterExpression(subParamName), null, tupleIndex + offset);
if (columnBindingMap != null) {
for (String column : nestedAttributeEntry.getColumnNames()) {
columnBindingMap.put(column, nestedAttributePath);
}
}
offset++;
}
parameterManager.getParameter(parameterName).setTransformer(new SplittingParameterTransformer(parameterManager, metamodel, elementType, parameterAccessPaths));
}
} else {
throw new IllegalArgumentException("Illegal expression '" + selectExpression.toString() + "' for binding relation '" + attributeName + "'!");
}
} else if (requiresNullCast && selectExpression instanceof NullExpression) {
if (BasicCastTypes.TYPES.contains(elementType) && queryBuilder.statementType != DbmsStatementType.INSERT) {
// We also need a cast for parameter expressions except in the SET clause
List<Expression> arguments = new ArrayList<>(2);
arguments.add(selectExpression);
if (columnType != null) {
arguments.add(new StringLiteral(columnType));
}
selectInfo.set(new FunctionExpression("CAST_" + elementType.getSimpleName(), arguments, selectExpression));
} else {
final EntityMetamodelImpl.AttributeExample attributeExample = metamodel.getBasicTypeExampleAttributes().get(elementType);
if (attributeExample != null) {
List<Expression> arguments = new ArrayList<>(2);
arguments.add(new SubqueryExpression(new Subquery() {
@Override
public String getQueryString() {
return attributeExample.getExampleJpql() + selectExpression;
}
}));
if (queryBuilder.statementType != DbmsStatementType.INSERT && needsCastParameters) {
arguments.add(new StringLiteral(attributeExample.getAttribute().getColumnTypes()[0]));
}
selectInfo.set(new FunctionExpression(NullfnFunction.FUNCTION_NAME, arguments, selectExpression));
}
}
} else if (selectExpression instanceof ParameterExpression && clause != ClauseType.SET) {
if (BasicCastTypes.TYPES.contains(elementType) && queryBuilder.statementType != DbmsStatementType.INSERT) {
// We also need a cast for parameter expressions except in the SET clause
List<Expression> arguments = new ArrayList<>(2);
arguments.add(selectExpression);
if (columnType != null) {
arguments.add(new StringLiteral(columnType));
}
selectInfo.set(new FunctionExpression("CAST_" + elementType.getSimpleName(), arguments, selectExpression));
} else {
final EntityMetamodelImpl.AttributeExample attributeExample = metamodel.getBasicTypeExampleAttributes().get(elementType);
if (attributeExample != null) {
List<Expression> arguments = new ArrayList<>(2);
arguments.add(new SubqueryExpression(new Subquery() {
@Override
public String getQueryString() {
return attributeExample.getExampleJpql() + selectExpression;
}
}));
if (queryBuilder.statementType != DbmsStatementType.INSERT && needsCastParameters) {
arguments.add(new StringLiteral(attributeExample.getAttribute().getColumnTypes()[0]));
}
selectInfo.set(new FunctionExpression(ParamFunction.FUNCTION_NAME, arguments, selectExpression));
}
}
}
}
}
use of com.blazebit.persistence.parser.expression.PathExpression in project blaze-persistence by Blazebit.
the class JoinManager method implicitJoin.
private JoinResult implicitJoin(JoinNode current, List<String> resultFields, PathExpression pathExpression, ClauseType fromClause, JoinType joinType, JoinNode currentJoinNode, Set<String> currentlyResolvingAliases, int start, int end, boolean allowParentAliases, boolean joinAllowed, boolean singularJoinAllowed, boolean idRemovable) {
List<PathElementExpression> pathElements = pathExpression.getExpressions();
PathElementExpression elementExpr;
int singleValuedAssociationNameStartIndex = -1;
int singleValuedAssociationNameEndIndex = -1;
for (int i = start; i < end; i++) {
AliasInfo aliasInfo;
elementExpr = pathElements.get(i);
if (elementExpr instanceof ArrayExpression) {
ArrayExpression arrayExpr = (ArrayExpression) elementExpr;
String joinRelationName;
List<String> joinRelationAttributes;
if (!resultFields.isEmpty()) {
resultFields.add(arrayExpr.getBase().toString());
joinRelationAttributes = resultFields;
resultFields = new ArrayList<>();
joinRelationName = StringUtils.join(".", joinRelationAttributes);
} else {
joinRelationName = arrayExpr.getBase().toString();
joinRelationAttributes = Arrays.asList(joinRelationName);
}
current = current == null ? getRootNodeOrFail("Ambiguous join path [", joinRelationName, "] because of multiple root nodes!") : current;
implicitJoinIndex(arrayExpr);
// Find a node by a predicate match
JoinNode matchingNode = findNode(current, joinRelationName, arrayExpr);
if (matchingNode != null) {
current = matchingNode;
} else if (i == 0 && (aliasInfo = aliasManager.getAliasInfoForBottomLevel(joinRelationName)) != null) {
// The first node is allowed to be a join alias
if (aliasInfo instanceof SelectInfo) {
throw new IllegalArgumentException("Illegal reference to the select alias '" + joinRelationName + "'");
}
current = ((JoinAliasInfo) aliasInfo).getJoinNode();
generateAndApplyOnPredicate(current, arrayExpr);
} else {
String joinAlias = getJoinAlias(arrayExpr);
if (arrayExpr.getBase() instanceof PropertyExpression) {
final JoinResult result = createOrUpdateNode(current, joinRelationAttributes, null, joinAlias, joinType, currentJoinNode, true, false, joinAllowed, singularJoinAllowed);
current = result.baseNode;
resultFields = result.addToList(resultFields);
} else {
joinAlias = aliasManager.generateJoinAlias(joinAlias);
Class<?> entityClass = ((EntityLiteral) arrayExpr.getBase()).getValue();
joinOn(null, current.getAlias(), entityClass, joinAlias, JoinType.LEFT, false).end();
current = ((JoinAliasInfo) aliasManager.getAliasInfo(joinAlias)).getJoinNode();
}
generateAndApplyOnPredicate(current, arrayExpr);
}
} else if (elementExpr instanceof TreatExpression) {
if (i != 0 || current != null) {
throw new IllegalArgumentException("A treat expression should be the first element in a path!");
}
TreatExpression treatExpression = (TreatExpression) elementExpr;
boolean fromSubquery = false;
boolean fromSelectAlias = false;
boolean joinRequired = false;
boolean fetch = false;
current = implicitJoinTreatExpression((TreatExpression) elementExpr, joinAllowed, singularJoinAllowed, fromClause, joinType, currentJoinNode, currentlyResolvingAliases, fromSubquery, fromSelectAlias, true, false, fetch, false);
} else if (elementExpr instanceof MapKeyExpression) {
MapKeyExpression mapKeyExpression = (MapKeyExpression) elementExpr;
boolean fromSubquery = false;
boolean fromSelectAlias = false;
boolean joinRequired = true;
boolean fetch = false;
current = joinMapKey(mapKeyExpression, null, fromClause, currentlyResolvingAliases, fromSubquery, fromSelectAlias, joinRequired, fetch, true, true);
} else if (elementExpr instanceof MapValueExpression) {
MapValueExpression mapValueExpression = (MapValueExpression) elementExpr;
boolean fromSubquery = false;
boolean fromSelectAlias = false;
boolean joinRequired = true;
boolean fetch = false;
implicitJoin(mapValueExpression.getPath(), joinAllowed, singularJoinAllowed, true, null, fromClause, currentlyResolvingAliases, fromSubquery, fromSelectAlias, joinRequired, fetch);
current = (JoinNode) mapValueExpression.getPath().getBaseNode();
} else if (pathElements.size() == 1 && (aliasInfo = aliasManager.getAliasInfoForBottomLevel(elementExpr.toString())) != null) {
if (aliasInfo instanceof SelectInfo) {
throw new IllegalArgumentException("Can't dereference a select alias");
} else {
// Join alias usage like in "joinAlias.relationName"
current = ((JoinAliasInfo) aliasInfo).getJoinNode();
}
} else {
String elementExpressionString = elementExpr.toString();
if (current == null) {
// When no base is given, check if the attribute name is an alias
aliasInfo = allowParentAliases ? aliasManager.getAliasInfo(elementExpressionString) : aliasManager.getAliasInfoForBottomLevel(elementExpressionString);
if (aliasInfo instanceof JoinAliasInfo) {
current = ((JoinAliasInfo) aliasInfo).getJoinNode();
// We can only "consider" this path a single valued association id when we are about to "remove" the id part
if (idRemovable && current.getNodeType() instanceof ManagedType<?> && pathElements.size() == i + 2) {
ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, current.getManagedType());
if (contains(managedType.getIdAttributes(), pathElements.get(i + 1))) {
singleValuedAssociationNameStartIndex = 0;
singleValuedAssociationNameEndIndex = 0;
break;
}
}
continue;
} else {
current = getRootNodeOrFail("Ambiguous join path [", elementExpressionString, "] because of multiple root nodes!");
}
}
int pathElementsSize = pathElements.size();
if (joinType != JoinType.INNER && (idRemovable || mainQuery.jpaProvider.supportsSingleValuedAssociationIdExpressions()) && (current.getManagedType().getPersistenceType() != Type.PersistenceType.EMBEDDABLE || current.getValuesLikeAttribute() != null) && i + 1 < pathElementsSize) {
// If the current type is not an embeddable, we check if the path elements access a singular association id
List<PathElementExpression> pathElementExpressions = new ArrayList<>(pathElementsSize - i);
for (int j = i; j < pathElementsSize; j++) {
PathElementExpression pathElementExpression = pathElements.get(j);
if (!(pathElementExpression instanceof PropertyExpression)) {
break;
}
pathElementExpressions.add(pathElementExpression);
}
ExtendedManagedType<?> extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, current.getManagedType());
// We collect and check if we have only property expressions
if (pathElementExpressions.size() == pathElementsSize - i) {
// Only if all path elements are property expressions, we check if this is single valued association id
PathExpression pathRestExpression = new PathExpression(pathElementExpressions);
String pathRestString = pathRestExpression.toString();
int idx = 0;
ExtendedAttribute<?, ?> extendedAttribute;
if (current.getValuesLikeAttribute() == null) {
extendedAttribute = extendedManagedType.getOwnedSingularAttributes().get(pathRestString);
} else {
extendedAttribute = extendedManagedType.getAttributes().get(pathRestString);
}
if (extendedAttribute != null && !JpaMetamodelUtils.isAssociation(extendedAttribute.getAttribute())) {
ExtendedAttribute<?, ?> associationAttribute = null;
ExtendedAttribute<?, ?> attr;
singleValuedAssociationNameStartIndex = i;
List<String> newResultFields = new ArrayList<>();
for (int j = i; j < end; j++) {
idx = pathRestString.indexOf('.', idx + 1);
if (idx != -1 && JpaMetamodelUtils.isAssociation((attr = extendedManagedType.getAttribute(pathRestString.substring(0, idx))).getAttribute())) {
associationAttribute = attr;
singleValuedAssociationNameEndIndex = j;
}
newResultFields.add(pathElements.get(j).toString());
}
if (singleValuedAssociationNameEndIndex == -1) {
// The expression ends at an association, so this can't be a single valued association id expression
singleValuedAssociationNameStartIndex = -1;
} else if (current.getValueType() == null && mainQuery.jpaProvider.isForeignJoinColumn((EntityType<?>) current.getManagedType(), new PathExpression(pathElements.subList(singleValuedAssociationNameStartIndex, singleValuedAssociationNameEndIndex + 1)).toString()) || current.getValueType() != null && mainQuery.jpaProvider.isForeignJoinColumn(current.getValueType(), current.getValuesLikeAttribute() + "." + new PathExpression(pathElements.subList(singleValuedAssociationNameStartIndex, singleValuedAssociationNameEndIndex + 1)).toString())) {
// If the column is "foreign", we can't do any optimizations
singleValuedAssociationNameStartIndex = -1;
} else if (!mainQuery.jpaProvider.supportsSingleValuedAssociationNaturalIdExpressions() && !contains(metamodel.getManagedType(ExtendedManagedType.class, associationAttribute.getElementClass()), new PathExpression(pathElements.subList(singleValuedAssociationNameEndIndex + 1, pathElementsSize)))) {
// If the jpa provider doesn't support any optimizations, we are done
singleValuedAssociationNameStartIndex = -1;
} else {
resultFields.addAll(newResultFields);
break;
}
}
}
}
if (resultFields.isEmpty()) {
final JoinResult result = implicitJoinSingle(current, elementExpressionString, null, joinType, currentJoinNode, allowParentAliases, joinAllowed, singularJoinAllowed);
if (current != result.baseNode) {
current = result.baseNode;
}
resultFields = result.addToList(resultFields);
} else {
resultFields.add(elementExpressionString);
JoinResult currentResult = createOrUpdateNode(current, resultFields, null, null, joinType, currentJoinNode, true, true, joinAllowed, singularJoinAllowed);
current = currentResult.baseNode;
if (!currentResult.hasField()) {
resultFields.clear();
}
}
}
}
if (resultFields.isEmpty()) {
return new JoinResult(current, null, current == null ? null : current.getNodeType(), singleValuedAssociationNameStartIndex, singleValuedAssociationNameEndIndex);
} else {
List<PathElementExpression> pathElementExpressions = new ArrayList<>(resultFields.size());
for (int i = 0; i < resultFields.size(); i++) {
pathElementExpressions.add(new PropertyExpression(resultFields.get(i)));
}
Expression expression = new PathExpression(pathElementExpressions);
Type<?> type = JpaUtils.getAttributeForJoining(metamodel, current.getNodeType(), expression, null).getAttributeType();
return new JoinResult(current, resultFields, type, singleValuedAssociationNameStartIndex, singleValuedAssociationNameEndIndex);
}
}
Aggregations