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
           
static String EXISTS_QUERY_STRING
           
static String READ_ALL_QUERY
           
 
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 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 String toJpaDirection(Sort.Order order)
           
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

EXISTS_QUERY_STRING

public static final String EXISTS_QUERY_STRING
See Also:
Constant Field Values

DELETE_ALL_QUERY_STRING

public static final String DELETE_ALL_QUERY_STRING
See Also:
Constant Field Values

READ_ALL_QUERY

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

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 -
alias -
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:

toJpaDirection

public static String toJpaDirection(Sort.Order order)

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:

Spring Data Jpa

Copyright © 2011-2012 SpringSource. All Rights Reserved.