Class QueryUtils
java.lang.Object
org.springframework.data.jpa.repository.query.QueryUtils
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
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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.static String
applySorting
(String query, org.springframework.data.domain.Sort sort) Adds order by clause to the JPQL query.static String
applySorting
(String query, org.springframework.data.domain.Sort sort, String alias) Adds order by clause to the JPQL query.static String
createCountQueryFor
(String originalQuery) Deprecated.static String
createCountQueryFor
(String originalQuery, String countProjection) Deprecated.useDeclaredQuery.deriveCountQuery(String, String)
instead.static String
detectAlias
(String query) Deprecated.useDeclaredQuery.getAlias()
instead.static String
getExistsQueryString
(String entityName, String countQueryPlaceHolder, Iterable<String> idAttributes) Returns the query string to execute an exists query for the given id attributes.static String
getProjection
(String query) Returns the projection part of the query, i.e. everything betweenselect
andfrom
.static String
getQueryString
(String template, String entityName) Returns the query string for the given class name.static boolean
hasConstructorExpression
(String query) Returns whether the given JPQL query contains a constructor expression.static boolean
hasNamedParameter
(jakarta.persistence.Query query) Returns whether the givenQuery
contains named parameters.static List<jakarta.persistence.criteria.Order>
toOrders
(org.springframework.data.domain.Sort sort, jakarta.persistence.criteria.From<?, ?> from, jakarta.persistence.criteria.CriteriaBuilder cb) Turns the givenSort
intoOrder
s.
-
Field Details
-
COUNT_QUERY_STRING
- See Also:
-
DELETE_ALL_QUERY_STRING
- See Also:
-
DELETE_ALL_QUERY_BY_ID_STRING
- See Also:
-
-
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
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
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 appliedsort
- the sort specification to apply.- Returns:
- the modified query string.
-
applySorting
public static String applySorting(String query, org.springframework.data.domain.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
Deprecated.useDeclaredQuery.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.useDeclaredQuery.deriveCountQuery(String, 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.useDeclaredQuery.deriveCountQuery(String, 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 givenQuery
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(org.springframework.data.domain.Sort sort, jakarta.persistence.criteria.From<?, ?> from, jakarta.persistence.criteria.CriteriaBuilder cb) Turns the givenSort
intoOrder
s.- Parameters:
sort
- theSort
instance to be transformed into JPAOrder
s.from
- must not be null.cb
- must not be null.- Returns:
- a
List
ofOrder
s.
-
hasConstructorExpression
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
Returns the projection part of the query, i.e. everything betweenselect
andfrom
.- Parameters:
query
- must not be null or empty.- Returns:
- the projection part of the query.
- Since:
- 1.10.2
-
DeclaredQuery.deriveCountQuery(String, String)
instead.