Class SearchQuery<T>


  • public class SearchQuery<T>
    extends Object
    A SearchQuery can be used to perform JPA-queries for JPA entities. Therefore multiple criteria can be added, which are being used in the WHERE-clause of the query (with an and-concatenation).
    Example:
    To retrieve all male users aged over 30 whose name starts with "John", the following query could be build:
     SearchQuery<User> searchQuery = userRepository.createSearchQuery();
     searchQuery.startsWith("name", "John").equals("gender", "male").greaterThan("age", 30);
     
    This would result in a JPA-query like this:
     from User e where e.name like ?1 and e.gender = ?2 and e.age > ?3
     
    The query can then be executed by calling execute(javax.persistence.EntityManager) or execute(org.springframework.data.domain.Pageable, javax.persistence.EntityManager) .

    This class can be sub-classed to implement custom behavior.

    Note that the methods which are used to add a criteria are null-safe, which means the criteria is ignored if the given value is null.
    Author:
    Matthias Müller
    See Also:
    SearchRepository.search(SearchQuery, Pageable)
    • Constructor Detail

      • SearchQuery

        public SearchQuery​(Class<T> domainClass)
        Creates a new SearchQuery for the given type.
        Parameters:
        domainClass - the type of the JPA Entity
    • Method Detail

      • isAppendEntityAlias

        public boolean isAppendEntityAlias()
        Checks whether or not to add the entity alias e. for each criteria in the JPQL query. Default is true .
        Returns:
        true when entity alias gets appended, false otherwise
      • and

        public SearchQuery<T> and​(String clause,
                                  Map<String,​Object> params)
        Adds a parameterized JPQL AND-clause to this query. Avoid using the same parameter names for different calls of this method.
        Usage example:
         SearchQuery query= repository.createSearchQuery();
         Map<String, Object> params = new HashMap<String, Object>();
         params.put("date", new Date());
         query.and("(e.valid_from before :date or e.valid_from is null)", params);
         
        Parameters:
        clause - the AND clause
        params - the named parameters for the AND-clause
        Returns:
        the current SearchQuery
        See Also:
        and(String), setAppendEntityAlias(boolean)
      • setAppendEntityAlias

        public void setAppendEntityAlias​(boolean appendEntityAlias)
        Set to false to avoid adding the entity alias e. for each criteria in the JPQL query.
        Parameters:
        appendEntityAlias -
        See Also:
        isAppendEntityAlias(), setEntityAlias(String)
      • getEntityAlias

        public String getEntityAlias()
        Gets the entity alias to by used in JPQL queries
        Returns:
        the alias
      • setEntityAlias

        public SearchQuery<T> setEntityAlias​(String entityAlias)
        Sets the entity alias to by used in JPQL queries
        Parameters:
        entityAlias - the alias
        Returns:
        the current SearchQuery
      • equals

        public final SearchQuery<T> equals​(String name,
                                           Object value)
        Checks if the attribute equals the given value.
        JPQL-fragment:
         e.name = :value
         
        Parameters:
        name - the name of the attribute
        value - the value to check
        Returns:
        the current SearchQuery
      • notEquals

        public final SearchQuery<T> notEquals​(String name,
                                              Object value)
        Checks if the attribute not equals the given value.
        JPQL-fragment:
         e.name != :value
         
        Parameters:
        name - the name of the attribute
        value - the value to check
        Returns:
        the current SearchQuery
      • greaterThan

        public final SearchQuery<T> greaterThan​(String name,
                                                Object value)
        Checks if the attribute is greater than the given value.
        JPQL-fragment:
         e.name > :value
         
        Parameters:
        name - the name of the attribute
        value - the value to check
        Returns:
        the current SearchQuery
      • greaterEquals

        public final SearchQuery<T> greaterEquals​(String name,
                                                  Object value)
        Checks if the attribute is greater or equals the given value.
        JPQL-fragment:
         e.name >= :value
         
        Parameters:
        name - the name of the attribute
        value - the value to check
        Returns:
        the current SearchQuery
      • lessThan

        public final SearchQuery<T> lessThan​(String name,
                                             Object value)
        Checks if the attribute is less than the given value.
        JPQL-fragment:
         e.name < :value
         
        Parameters:
        name - the name of the attribute
        value - the value to check
        Returns:
        the current SearchQuery
      • lessEquals

        public final SearchQuery<T> lessEquals​(String name,
                                               Object value)
        Checks if the attribute is less or equals the given value.
        JPQL-fragment:
         e.name <= :value
         
        Parameters:
        name - the name of the attribute
        value - the value to check
        Returns:
        the current SearchQuery
      • in

        public final SearchQuery<T> in​(String name,
                                       Collection<?> values)
        Checks if the attribute is in the given values.
        JPQL-fragment:
         e.name in :value
         
        Parameters:
        name - the name of the attribute
        values - the values to check
        Returns:
        the current SearchQuery
      • notIn

        public final SearchQuery<T> notIn​(String name,
                                          Collection<?> values)
        Checks if the attribute is not in the given values.
        JPQL-fragment:
         e.name not in :value
         
        Parameters:
        name - the name of the attribute
        values - the values to check
        Returns:
        the current SearchQuery
      • contains

        public final SearchQuery<T> contains​(String name,
                                             Object value)
        Checks if the attribute is contains the given value.
        JPQL-fragment:
         e.name like :value
         
        You don't have to add the wildcard '%' before and after the value, this is done internally!
        Parameters:
        name - the name of the attribute
        value - the value to check
        Returns:
        the current SearchQuery
      • startsWith

        public final SearchQuery<T> startsWith​(String name,
                                               Object value)
        Checks if the attribute is starts with the given value.
        JPQL-fragment:
         e.name like :value
         
        You don't have to add the wildcard '%' after the value, this is done internally!
        Parameters:
        name - the name of the attribute
        value - the value to check
        Returns:
        the current SearchQuery
        See Also:
        like(String, Object)
      • endsWith

        public final SearchQuery<T> endsWith​(String name,
                                             Object value)
        Checks if the attribute is ends with the given value.
        JPQL-fragment:
         e.name like :value
         
        You don't have to add the wildcard '%' before the value, this is done internally!
        Parameters:
        name - the name of the attribute
        value - the value to check
        Returns:
        the current SearchQuery
        See Also:
        like(String, Object)
      • isNotNull

        public final SearchQuery<T> isNotNull​(String name)
        Checks if the attribute is not null.
        JPQL-fragment:
         e.name is not null
         
        Parameters:
        name - the name of the attribute
        Returns:
        the current SearchQuery
      • isNull

        public final SearchQuery<T> isNull​(String name)
        Checks if the attribute is null.
        JPQL-fragment:
         e.name is null
         
        Parameters:
        name - the name of the attribute
        Returns:
        the current SearchQuery
      • appendOrder

        protected void appendOrder​(org.springframework.data.domain.Pageable pageable,
                                   StringBuilder queryBuilder,
                                   String entityName)
        Appends an ORDER BY clause, derived from the pageable, to the queryBuilder, using the given entityName.
        Parameters:
        pageable - the Pageable, may be null
        queryBuilder - the query builder
        entityName - the name for the entity
      • appendOrder

        protected void appendOrder​(org.springframework.data.domain.Sort sort,
                                   StringBuilder queryBuilder,
                                   String entityName)
        Appends an ORDER BY clause, derived from the sort, to the queryBuilder, using the given entityName.
        Parameters:
        sort - the Sort, may be null
        queryBuilder - the query builder
        entityName - the name for the entity
      • join

        public SearchQuery<T> join​(String joinQuery)
        Adds one or more joins to the resulting JPQL-query.
        The alias for the entity is e, so you could add a join like
         join e.addresses a
         
        Parameters:
        joinQuery - the join-part of a JPQL query
        Returns:
        the current SearchQuery
      • distinct

        public SearchQuery<T> distinct()
        Causes the JPQL-query to select distinct entities only.
        Returns:
        the current SearchQuery
      • execute

        public List<T> execute​(javax.persistence.EntityManager entityManager)
        Executes this SearchQuery with the given EntityManager.
        Parameters:
        entityManager - the EntityManager
        Returns:
        the result-list
      • execute

        public org.springframework.data.domain.Page<T> execute​(org.springframework.data.domain.Pageable pageable,
                                                               javax.persistence.EntityManager entityManager)
        Executes this SearchQuery with the given EntityManager, applying the given Pageable (if any) for paging and sorting the results.
        Parameters:
        pageable - a Pageable (optional)
        entityManager - the EntityManager
        Returns:
        the result-Page
        See Also:
        SearchRepository.search(SearchQuery, Pageable)
      • appendJoinAndWhereClause

        protected StringBuilder appendJoinAndWhereClause​(StringBuilder queryBuilder)
        Appends a JOIN-clause (if present) and the WHERE-clause to the given queryBuilder
        Parameters:
        queryBuilder - the StringBuilder used to build the query
        Returns:
        the queryBuilder
        See Also:
        join(String)
      • setQueryParameters

        protected SearchQuery<T> setQueryParameters​(javax.persistence.Query... queries)
        Sets the parameters defined by criteria for the given queries.
        Parameters:
        queries -
        Returns:
        the current SearchQuery
      • applyPagination

        protected org.springframework.data.domain.Pageable applyPagination​(javax.persistence.Query query,
                                                                           Long total,
                                                                           org.springframework.data.domain.Pageable pageable)
        Applies the given pageable to the query. If the pageable's offset is >= total, a new Pageable starting at page 0 is returned.
        Parameters:
        query - the Query
        total - the total number of items
        pageable - the Pageable
        Returns:
        a (possibly new) Pageable