use of org.springframework.data.mapping.PropertyPath in project spring-data-jpa by spring-projects.
the class QueryUtilsIntegrationTests method reusesExistingJoinForExpression.
// DATAJPA-403
@Test
void reusesExistingJoinForExpression() {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User> query = builder.createQuery(User.class);
Root<User> from = query.from(User.class);
PropertyPath managerFirstname = PropertyPath.from("manager.firstname", User.class);
PropertyPath managerLastname = PropertyPath.from("manager.lastname", User.class);
QueryUtils.toExpressionRecursively(from, managerLastname);
QueryUtils.toExpressionRecursively(from, managerFirstname);
assertThat(from.getJoins()).hasSize(1);
}
use of org.springframework.data.mapping.PropertyPath in project spring-data-jpa by spring-projects.
the class Querydsl method buildOrderPropertyPathFrom.
/**
* Creates an {@link Expression} for the given {@link Order} property.
*
* @param order must not be {@literal null}.
* @return
*/
private Expression<?> buildOrderPropertyPathFrom(Order order) {
Assert.notNull(order, "Order must not be null!");
PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType());
Expression<?> sortPropertyExpression = builder;
while (path != null) {
sortPropertyExpression = //
!path.hasNext() && order.isIgnoreCase() && String.class.equals(path.getType()) ? //
Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower() : Expressions.path(path.getType(), (Path<?>) sortPropertyExpression, path.getSegment());
path = path.next();
}
return sortPropertyExpression;
}
use of org.springframework.data.mapping.PropertyPath in project spring-data-jpa by spring-projects.
the class QueryUtils method toExpressionRecursively.
/**
* Creates an expression with proper inner and left joins by recursively navigating the path
*
* @param from the {@link From}
* @param property the property path
* @param isForSelection is the property navigated for the selection or ordering part of the query?
* @param hasRequiredOuterJoin has a parent already required an outer join?
* @param <T> the type of the expression
* @return the expression
*/
@SuppressWarnings("unchecked")
static <T> Expression<T> toExpressionRecursively(From<?, ?> from, PropertyPath property, boolean isForSelection, boolean hasRequiredOuterJoin) {
String segment = property.getSegment();
boolean isLeafProperty = !property.hasNext();
boolean requiresOuterJoin = requiresOuterJoin(from, property, isForSelection, hasRequiredOuterJoin);
// if it does not require an outer join and is a leaf, simply get the segment
if (!requiresOuterJoin && isLeafProperty) {
return from.get(segment);
}
// get or create the join
JoinType joinType = requiresOuterJoin ? JoinType.LEFT : JoinType.INNER;
Join<?, ?> join = getOrCreateJoin(from, segment, joinType);
// if it's a leaf, return the join
if (isLeafProperty) {
return (Expression<T>) join;
}
PropertyPath nextProperty = Objects.requireNonNull(property.next(), "An element of the property path is null!");
// recurse with the next property
return toExpressionRecursively(join, nextProperty, isForSelection, requiresOuterJoin);
}
use of org.springframework.data.mapping.PropertyPath in project spring-graphql by spring-projects.
the class PropertySelection method collectPropertyPaths.
private static List<PropertyPath> collectPropertyPaths(TypeInformation<?> typeInformation, FieldSelection selection, Function<String, PropertyPath> propertyPathFactory) {
List<PropertyPath> propertyPaths = new ArrayList<>();
for (SelectedField selectedField : selection) {
String propertyName = selectedField.getName();
TypeInformation<?> property = typeInformation.getProperty(propertyName);
if (property == null) {
continue;
}
PropertyPath propertyPath = propertyPathFactory.apply(propertyName);
FieldSelection nestedSelection = selection.select(selectedField);
List<PropertyPath> pathsToAdd = Collections.singletonList(propertyPath);
if (!nestedSelection.isEmpty() && property.getActualType() != null) {
List<PropertyPath> nestedPaths = collectPropertyPaths(property.getRequiredActualType(), nestedSelection, propertyPath::nested);
if (!nestedPaths.isEmpty()) {
pathsToAdd = nestedPaths;
}
}
propertyPaths.addAll(pathsToAdd);
}
return propertyPaths;
}
use of org.springframework.data.mapping.PropertyPath in project spring-data-keyvalue by spring-projects.
the class KeyValueQuerydslUtils method buildOrderPropertyPathFrom.
/**
* Creates an {@link Expression} for the given {@link Order} property.
*
* @param order must not be {@literal null}.
* @param builder must not be {@literal null}.
* @return
*/
private static Expression<?> buildOrderPropertyPathFrom(Order order, PathBuilder<?> builder) {
Assert.notNull(order, "Order must not be null!");
Assert.notNull(builder, "Builder must not be null!");
PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType());
Expression<?> sortPropertyExpression = builder;
while (path != null) {
if (!path.hasNext() && order.isIgnoreCase()) {
// if order is ignore-case we have to treat the last path segment as a String.
sortPropertyExpression = Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower();
} else {
sortPropertyExpression = Expressions.path(path.getType(), (Path<?>) sortPropertyExpression, path.getSegment());
}
path = path.next();
}
return sortPropertyExpression;
}
Aggregations