Search in sources :

Example 11 with PropertyPath

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);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) User(org.springframework.data.jpa.domain.sample.User) PropertyPath(org.springframework.data.mapping.PropertyPath) Test(org.junit.jupiter.api.Test)

Example 12 with PropertyPath

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;
}
Also used : Path(com.querydsl.core.types.Path) PropertyPath(org.springframework.data.mapping.PropertyPath) EntityPath(com.querydsl.core.types.EntityPath) PropertyPath(org.springframework.data.mapping.PropertyPath)

Example 13 with PropertyPath

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);
}
Also used : Expression(javax.persistence.criteria.Expression) PropertyPath(org.springframework.data.mapping.PropertyPath) JoinType(javax.persistence.criteria.JoinType)

Example 14 with PropertyPath

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;
}
Also used : SelectedField(graphql.schema.SelectedField) PropertyPath(org.springframework.data.mapping.PropertyPath) ArrayList(java.util.ArrayList)

Example 15 with PropertyPath

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;
}
Also used : Path(com.querydsl.core.types.Path) PropertyPath(org.springframework.data.mapping.PropertyPath) PropertyPath(org.springframework.data.mapping.PropertyPath)

Aggregations

PropertyPath (org.springframework.data.mapping.PropertyPath)20 ArrayList (java.util.ArrayList)6 Test (org.junit.jupiter.api.Test)4 Path (com.querydsl.core.types.Path)3 QueryClause (com.redis.om.spring.repository.query.clause.QueryClause)2 Neo4jPersistentProperty (org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty)2 Person (org.springframework.data.neo4j.integration.shared.common.Person)2 Neo4jIntegrationTest (org.springframework.data.neo4j.test.Neo4jIntegrationTest)2 Part (org.springframework.data.repository.query.parser.Part)2 Pair (org.springframework.data.util.Pair)2 Ref (com.arangodb.springframework.annotation.Ref)1 Relations (com.arangodb.springframework.annotation.Relations)1 GeoJson (com.arangodb.springframework.core.geo.GeoJson)1 ArangoMappingContext (com.arangodb.springframework.core.mapping.ArangoMappingContext)1 ArangoPersistentProperty (com.arangodb.springframework.core.mapping.ArangoPersistentProperty)1 AqlUtils (com.arangodb.springframework.core.util.AqlUtils)1 ArangoParameterAccessor (com.arangodb.springframework.repository.query.ArangoParameterAccessor)1 Ring (com.arangodb.springframework.repository.query.derived.geo.Ring)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1