java.lang.Object
org.springframework.data.jpa.repository.query.QueryUtils

public abstract class QueryUtils extends Object
Simple utility class to create JPA queries using the default implementation of a custom parser.
Author:
Oliver Gierke, Kevin Raymond, Thomas Darimont, Komi Innocent, Christoph Strobl, Mark Paluch, Sébastien Péralta, Jens Schauder, Nils Borrmann, Reda.Housni-Alaoui, Florian Lüdiger, Grégoire Druant, Mohammad Hewedy, Andriy Redko, Peter Großmann, Greg Turnquist, Diego Krupitza, Jędrzej Biedrzycki, Darin Manica, Simon Paradies, Vladislav Yukharin, Chris Fraser, Donghun Shin, Pranav HS, Eduard Dudar, Yanming Zhou
  • Field Details

  • Method Details

    • getExistsQueryString

      public static String getExistsQueryString(String entityName, String countQueryPlaceHolder, Iterable<String> idAttributes)
      Returns the query string to execute an exists query for the given id attributes.
      Parameters:
      entityName - the name of the entity to create the query for, must not be null.
      countQueryPlaceHolder - the placeholder for the count clause, must not be null.
      idAttributes - the id attributes for the entity, must not be null.
    • getQueryString

      public static String getQueryString(String template, String entityName)
      Returns the query string for the given class name.
      Parameters:
      template - must not be null.
      entityName - must not be null.
      Returns:
      the template with placeholders replaced by the entityName. Guaranteed to be not null.
    • applySorting

      public static String applySorting(String query, Sort sort)
      Adds order by clause to the JPQL query. Uses the first alias to bind the sorting property to.
      Parameters:
      query - the query string to which sorting is applied
      sort - the sort specification to apply.
      Returns:
      the modified query string.
    • applySorting

      public static String applySorting(String query, Sort sort, @Nullable String alias)
      Adds order by clause to the JPQL query.
      Parameters:
      query - the query string to which sorting is applied. Must not be null or empty.
      sort - the sort specification to apply.
      alias - the alias to be used in the order by clause. May be null or empty.
      Returns:
      the modified query string.
    • detectAlias

      @Nullable @Deprecated public static String detectAlias(String query)
      Deprecated.
      use DeclaredQuery.getAlias() instead.
      Resolves the alias for the entity to be retrieved from the given JPA query.
      Parameters:
      query - must not be null.
      Returns:
      Might return null.
    • applyAndBind

      public static <T> jakarta.persistence.Query applyAndBind(String queryString, Iterable<T> entities, jakarta.persistence.EntityManager entityManager)
      Creates a where-clause referencing the given entities and appends it to the given query string. Binds the given entities to the query.
      Type Parameters:
      T - type of the entities.
      Parameters:
      queryString - must not be null.
      entities - must not be null.
      entityManager - must not be null.
      Returns:
      Guaranteed to be not null.
    • createCountQueryFor

      @Deprecated public static String createCountQueryFor(String originalQuery)
      Deprecated.
      use DeclaredQuery.deriveCountQuery(String) instead.
      Creates a count projected query from the given original query.
      Parameters:
      originalQuery - must not be null or empty.
      Returns:
      Guaranteed to be not null.
    • createCountQueryFor

      @Deprecated public static String createCountQueryFor(String originalQuery, @Nullable String countProjection)
      Deprecated.
      use DeclaredQuery.deriveCountQuery(String) instead.
      Creates a count projected query from the given original query.
      Parameters:
      originalQuery - must not be null.
      countProjection - may be null.
      Returns:
      a query String to be used a count query for pagination. Guaranteed to be not null.
      Since:
      1.6
    • hasNamedParameter

      public static boolean hasNamedParameter(jakarta.persistence.Query query)
      Returns whether the given Query contains named parameters.
      Parameters:
      query - Must not be null.
      Returns:
      whether the given Query contains named parameters.
    • toOrders

      public static List<jakarta.persistence.criteria.Order> toOrders(Sort sort, jakarta.persistence.criteria.From<?,?> from, jakarta.persistence.criteria.CriteriaBuilder cb)
      Turns the given Sort into Orders.
      Parameters:
      sort - the Sort instance to be transformed into JPA Orders.
      from - must not be null.
      cb - must not be null.
      Returns:
      a List of Orders.
    • hasConstructorExpression

      public static boolean hasConstructorExpression(String query)
      Returns whether the given JPQL query contains a constructor expression.
      Parameters:
      query - must not be null or empty.
      Returns:
      whether the given JPQL query contains a constructor expression.
      Since:
      1.10
    • getProjection

      public static String getProjection(String query)
      Returns the projection part of the query, i.e. everything between select and from.
      Parameters:
      query - must not be null or empty.
      Returns:
      the projection part of the query.
      Since:
      1.10.2