DATAJPA-1238 - Avoid outer join when entity valued attribute is queried.

This avoids an extra join for derived queries that use a single valued entity in a where clause.
If this actually reduces the number of joins used in the SQL statement still depends on the JPA implementation.

Original pull request: #270.
This commit is contained in:
Réda Housni Alaoui
2018-04-24 15:46:30 +02:00
committed by Jens Schauder
parent fb17b874b0
commit ff6e6102e7
2 changed files with 8 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2017 the original author or authors.
* Copyright 2008-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -572,7 +572,7 @@ public abstract class QueryUtils {
propertyPathModel = from.get(segment).getModel();
}
if (requiresJoin(propertyPathModel, model instanceof PluralAttribute) && !isAlreadyFetched(from, segment)) {
if (requiresJoin(propertyPathModel, model instanceof PluralAttribute, !property.hasNext()) && !isAlreadyFetched(from, segment)) {
Join<?, ?> join = getOrCreateJoin(from, segment);
return (Expression<T>) (property.hasNext() ? toExpressionRecursively(join, property.next()) : join);
} else {
@@ -587,9 +587,10 @@ public abstract class QueryUtils {
*
* @param propertyPathModel must not be {@literal null}.
* @param forPluralAttribute
* @param forLeafProperty
* @return
*/
private static boolean requiresJoin(Bindable<?> propertyPathModel, boolean forPluralAttribute) {
private static boolean requiresJoin(Bindable<?> propertyPathModel, boolean forPluralAttribute, boolean forLeafProperty) {
if (propertyPathModel == null && forPluralAttribute) {
return true;
@@ -604,6 +605,9 @@ public abstract class QueryUtils {
if (!ASSOCIATION_TYPES.containsKey(attribute.getPersistentAttributeType())) {
return false;
}
if (forLeafProperty && !attribute.isCollection()) {
return false;
}
Class<? extends Annotation> associationAnnotation = ASSOCIATION_TYPES.get(attribute.getPersistentAttributeType());

View File

@@ -89,7 +89,7 @@ public class QueryUtilsIntegrationTests {
CriteriaQuery<User> query = builder.createQuery(User.class);
Root<User> root = query.from(User.class);
QueryUtils.toExpressionRecursively(root, PropertyPath.from("manager", User.class));
QueryUtils.toExpressionRecursively(root, PropertyPath.from("manager.firstname", User.class));
assertThat(root.getJoins(), hasSize(1));
}