Spring Data JPA

org.springframework.data.jpa.repository.query
Class QueryUtils

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

public abstract class QueryUtils
extends Object

Simple utility class to create JPA queries.

Author:
Oliver Gierke

Field Summary
static String COUNT_QUERY_STRING
           
static String DELETE_ALL_QUERY_STRING
           
 
Method Summary
static
<T> javax.persistence.Query
applyAndBind(String queryString, Iterable<T> entities, javax.persistence.EntityManager entityManager)
          Creates a where-clause referencing the given entities and appends it to the given query string.
static String applySorting(String query, Sort sort)
          Adds order by clause to the JPQL query.
static String applySorting(String query, Sort sort, String alias)
          Adds order by clause to the JPQL query.
static String createCountQueryFor(String originalQuery)
          Creates a count projected query from the given orginal query.
static String detectAlias(String query)
          Resolves the alias for the entity to be retrieved from the given JPA query.
static Long executeCountQuery(javax.persistence.TypedQuery<Long> query)
          Executes a count query and transparently sums up all values returned.
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 getQueryString(String template, String entityName)
          Returns the query string for the given class name.
static boolean hasNamedParameter(javax.persistence.Query query)
          Returns whether the given Query contains named parameters.
static List<javax.persistence.criteria.Order> toOrders(Sort sort, javax.persistence.criteria.Root<?> root, javax.persistence.criteria.CriteriaBuilder cb)
          Turns the given Sort into Orders.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COUNT_QUERY_STRING

public static final String COUNT_QUERY_STRING
See Also:
Constant Field Values

DELETE_ALL_QUERY_STRING

public static final String DELETE_ALL_QUERY_STRING
See Also:
Constant Field Values
Method Detail

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.
Returns:

getQueryString

public static String getQueryString(String template,
                                    String entityName)
Returns the query string for the given class name.

Parameters:
template -
entityName -
Returns:

applySorting

public static String applySorting(String query,
                                  Sort sort)
Adds order by clause to the JPQL query. Uses the DEFAULT_ALIAS to bind the sorting property to.

Parameters:
query -
sort -
Returns:

applySorting

public static String applySorting(String query,
                                  Sort sort,
                                  String alias)
Adds order by clause to the JPQL query.

Parameters:
query -
sort -
alias -
Returns:

detectAlias

public static String detectAlias(String query)
Resolves the alias for the entity to be retrieved from the given JPA query.

Parameters:
query -
Returns:

applyAndBind

public static <T> javax.persistence.Query applyAndBind(String queryString,
                                                       Iterable<T> entities,
                                                       javax.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 -
Parameters:
queryString -
entities -
entityManager -
Returns:

createCountQueryFor

public static String createCountQueryFor(String originalQuery)
Creates a count projected query from the given orginal query.

Parameters:
originalQuery - must not be null or empty
Returns:

hasNamedParameter

public static boolean hasNamedParameter(javax.persistence.Query query)
Returns whether the given Query contains named parameters.

Parameters:
query -
Returns:

toOrders

public static List<javax.persistence.criteria.Order> toOrders(Sort sort,
                                                              javax.persistence.criteria.Root<?> root,
                                                              javax.persistence.criteria.CriteriaBuilder cb)
Turns the given Sort into Orders.

Parameters:
sort - the Sort instance to be transformed into JPA Orders.
root - must not be null.
cb - must not be null.
Returns:

executeCountQuery

public static Long executeCountQuery(javax.persistence.TypedQuery<Long> query)
Executes a count query and transparently sums up all values returned.

Parameters:
query - must not be null.
Returns:

Spring Data JPA

Copyright © 2011-2013-2013 SpringSource. All Rights Reserved.