Class Expressions

java.lang.Object
org.springframework.data.jpa.criteria.Expressions

public abstract class Expressions extends Object
Utility methods to resolve JPA Criteria API objects using Spring Data's type-safe property references. These helper methods obtain Criteria API objects using TypedPropertyPath and PropertyReference to resolve property expressions and joins.

The class is intended for concise, type-aware criteria construction through a type-safe DSL where property references are preferred over string-based navigation.

Example:

Root<User> root = criteriaQuery.from(User.class);

Expression<User> expr = Expressions.get(root, User::getManager);

Join<User, Address> join = Expressions.join(root, JoinType.INNER, j -> j.join(User::getAddress));
Since:
4.1
Author:
Mark Paluch
See Also:
  • Method Details

    • path

      public static <T,P> Path<P> path(Path<T> from, TypedPropertyPath<T,P> property)
      Resolve a Path for the given property path.

      The resulting path can be used in predicates. In case of a From path, Expression resolution navigates joins as necessary.

      Parameters:
      from - the entity or attribute path start from.
      property - property path to navigate.
      Returns:
      the resolved path.
    • get

      public static <T,P> Expression<P> get(From<?,T> from, TypedPropertyPath<T,P> property)
      Resolve an Expression for the given property path.

      The resulting expression can be used in predicates. Expression resolution navigates joins as necessary.

      Parameters:
      from - the root or join to start from.
      property - property path to navigate.
      Returns:
      the resolved expression.
    • select

      public static <T,P> Selection<P> select(From<?,T> from, TypedPropertyPath<T,P> property)
      Create a Selection for the given property path.

      The resulting object can be used in the selection, joined paths consider outer joins as needed.

      Parameters:
      from - the root or join to start from.
      property - property path to navigate.
      Returns:
      the selection.
    • select

      @SafeVarargs public static <T> List<Selection<?>> select(From<?,T> from, TypedPropertyPath<T,?>... properties)
      Create a list of selection objects for the given property paths.

      The resulting objects can be used in the selection, joined paths consider outer joins as needed.

      Parameters:
      from - the root or join to start from.
      properties - property path to navigate.
      Returns:
      the selection.
    • join

      public static <T,P> Join<T,P> join(From<?,T> from, PropertyReference<T,P> property)
      Create a Join using the given property.
      Parameters:
      from - the root or join to start from.
      property - property reference to navigate.
      Returns:
      the resolved join.
      See Also:
    • join

      public static <T, J extends Join<?,?>> J join(From<?,T> from, JoinType joinType, Function<Expressions.Joiner<T>, J> function)
      Create a Join considering JoinType using the given joiner function allowing to express joins using property references.
      Parameters:
      from - the root or join to start from.
      joinType - the join type.
      function - joiner function.
      Returns:
      the resolved join.
      See Also:
    • fetch

      public static <T,P> Fetch<T,P> fetch(From<?,T> from, PropertyReference<T,P> property)
      Create a fetch join using the given property.
      Parameters:
      from - the root or join to start from.
      property - property reference to navigate.
      Returns:
      the resolved fetch.
      See Also:
    • fetch

      public static <T, F extends Fetch<?,?>> F fetch(From<?,T> from, JoinType joinType, Function<Expressions.Fetcher<T>, F> function)
      Create a fetch join considering JoinType using the given fetcher function allowing to express fetches using property references.
      Parameters:
      from - the root or join to start from.
      joinType - the join type.
      function - fetcher function.
      Returns:
      the resolved fetch.
      See Also: