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, Donghun Shin, Pranav HS, Eduard Dudar, Yanming Zhou, Alim Naizabek, Jakub Soltys
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> jakarta.persistence.QueryapplyAndBind(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 StringapplySorting(String query, Sort sort) Adds order by clause to the JPQL query.static StringapplySorting(String query, Sort sort, @Nullable String alias) Adds order by clause to the JPQL query.static voidcheckSortExpression(Sort.Order order) Check any givenJpaSort.JpaOrder.isUnsafe()order for presence of at least one property offending thePUNCTATION_PATTERNand throw anExceptionindicating potential unsafe order by expression.static StringcreateCountQueryFor(String originalQuery, @Nullable String countProjection, boolean nativeQuery) Creates a count projected query from the given original query.static StringgetExistsQueryString(String entityName, String countQueryPlaceHolder, Iterable<String> idAttributes) Returns the query string to execute an exists query for the given id attributes.static StringgetProjection(String query) Returns the projection part of the query, i.e. everything betweenselectandfrom.static StringgetQueryString(String template, String entityName) Returns the query string for the given class name.static booleanhasConstructorExpression(String query) Returns whether the given JPQL query contains a constructor expression.static booleanhasNamedParameter(jakarta.persistence.Query query) Returns whether the givenQuerycontains named parameters.static <T> jakarta.persistence.criteria.Expression<T> toExpressionRecursively(jakarta.persistence.criteria.From<?, ?> from, PropertyPath property, boolean isForSelection) static List<jakarta.persistence.criteria.Order> toOrders(Sort sort, jakarta.persistence.criteria.From<?, ?> from, jakarta.persistence.criteria.CriteriaBuilder cb) Turns the givenSortintoOrders.
-
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
-
applySorting
-
applySorting
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.
-
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
public static String createCountQueryFor(String originalQuery, @Nullable String countProjection, boolean nativeQuery) Creates a count projected query from the given original query.- Parameters:
originalQuery- must not be null.countProjection- may be null.nativeQuery- whether the underlying query is a native query.- Returns:
- a query String to be used a count query for pagination. Guaranteed to be not null.
- Since:
- 2.7.8
-
hasNamedParameter
public static boolean hasNamedParameter(jakarta.persistence.Query query) Returns whether the givenQuerycontains named parameters.- Parameters:
query- Must not be null.- Returns:
- whether the given
Querycontains named parameters.
-
toOrders
-
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
-
checkSortExpression
Check any givenJpaSort.JpaOrder.isUnsafe()order for presence of at least one property offending thePUNCTATION_PATTERNand throw anExceptionindicating potential unsafe order by expression.- Parameters:
order-
-
toExpressionRecursively
public static <T> jakarta.persistence.criteria.Expression<T> toExpressionRecursively(jakarta.persistence.criteria.From<?, ?> from, PropertyPath property, boolean isForSelection)
-