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:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Strategy interface used by fetch(From, PropertyReference) to obtain fetch joins using property references.
    static interface 
    Strategy interface used by join(From, PropertyReference) to obtain joins using property references.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T, F extends jakarta.persistence.criteria.Fetch<?,?>>
    F
    fetch(jakarta.persistence.criteria.From<?,T> from, jakarta.persistence.criteria.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.
    static <T,P> jakarta.persistence.criteria.Fetch<T,P>
    fetch(jakarta.persistence.criteria.From<?,T> from, PropertyReference<T,P> property)
    Create a fetch join using the given property.
    static <T,P> jakarta.persistence.criteria.Expression<P>
    get(jakarta.persistence.criteria.From<?,T> from, TypedPropertyPath<T,P> property)
    Resolve an Expression for the given property path.
    static <T, J extends jakarta.persistence.criteria.Join<?,?>>
    J
    join(jakarta.persistence.criteria.From<?,T> from, jakarta.persistence.criteria.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.
    static <T,P> jakarta.persistence.criteria.Join<T,P>
    join(jakarta.persistence.criteria.From<?,T> from, PropertyReference<T,P> property)
    Create a Join using the given property.
    static <T,P> jakarta.persistence.criteria.Path<P>
    path(jakarta.persistence.criteria.Path<T> from, TypedPropertyPath<T,P> property)
    Resolve a Path for the given property path.
    static <T> List<jakarta.persistence.criteria.Selection<?>>
    select(jakarta.persistence.criteria.From<?,T> from, TypedPropertyPath<T,?>... properties)
    Create a list of selection objects for the given property paths.
    static <T,P> jakarta.persistence.criteria.Selection<P>
    select(jakarta.persistence.criteria.From<?,T> from, TypedPropertyPath<T,P> property)
    Create a Selection for the given property path.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • path

      public static <T,P> jakarta.persistence.criteria.Path<P> path(jakarta.persistence.criteria.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> jakarta.persistence.criteria.Expression<P> get(jakarta.persistence.criteria.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> jakarta.persistence.criteria.Selection<P> select(jakarta.persistence.criteria.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<jakarta.persistence.criteria.Selection<?>> select(jakarta.persistence.criteria.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> jakarta.persistence.criteria.Join<T,P> join(jakarta.persistence.criteria.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:
      • From.join(String)
    • join

      public static <T, J extends jakarta.persistence.criteria.Join<?,?>> J join(jakarta.persistence.criteria.From<?,T> from, jakarta.persistence.criteria.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:
      • From.join(String, JoinType)
    • fetch

      public static <T,P> jakarta.persistence.criteria.Fetch<T,P> fetch(jakarta.persistence.criteria.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:
      • FetchParent.fetch(String)
    • fetch

      public static <T, F extends jakarta.persistence.criteria.Fetch<?,?>> F fetch(jakarta.persistence.criteria.From<?,T> from, jakarta.persistence.criteria.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:
      • FetchParent.fetch(String, JoinType)