public class SearchQuery<T> extends Object
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).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 > ?3The 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 isnull
.SearchRepository.search(SearchQuery, Pageable)
Modifier and Type | Class and Description |
---|---|
protected class |
SearchQuery.Clause
A part of a JPQL query that provides it's own parameters.
|
protected class |
SearchQuery.Criterion
A criterion consisting of the property's name and and
SearchQuery.Operand , optionally providing a value (it depends
on the operand if a value is needed). |
protected static class |
SearchQuery.Operand
An operand that is applied to a
SearchQuery.Criterion . |
Modifier and Type | Field and Description |
---|---|
protected static String |
AND |
protected List<SearchQuery.Clause> |
andClauses |
protected boolean |
appendEntityAlias |
protected List<SearchQuery.Criterion> |
criteria |
protected boolean |
distinct |
protected Class<T> |
domainClass |
protected static String |
DOT |
protected String |
entityAlias |
protected String |
joinQuery |
protected static String |
PERCENT |
protected static String |
WHERE |
Constructor and Description |
---|
SearchQuery(Class<T> domainClass)
Creates a new
SearchQuery for the given type. |
Modifier and Type | Method and Description |
---|---|
void |
and(String clause)
Adds a parameterless JPQL AND-clause to this query.
Usage example: |
void |
and(String clause,
Map<String,Object> params)
Adds a parameterized JPQL AND-clause to this query.
|
protected StringBuilder |
appendJoinAndWhereClause(StringBuilder queryBuilder)
Appends a JOIN-clause (if present) and the WHERE-clause to the given
queryBuilder |
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 . |
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 . |
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. |
protected StringBuilder |
buildQueryString()
Builds and returns the JPQL query string based on
the
domainClass
the entityAlias and appendEntityAlias
the joinQuery
the criteria
the andClauses
|
SearchQuery<T> |
contains(String name,
Object value)
Checks if the attribute is contains the given value.
JPQL-fragment: |
void |
distinct()
Causes the JPQL-query to select
distinct entities only. |
SearchQuery<T> |
endsWith(String name,
Object value)
Checks if the attribute is ends with the given value.
JPQL-fragment: |
SearchQuery<T> |
equals(String name,
Object value)
Checks if the attribute equals the given value.
JPQL-fragment: |
List<T> |
execute(javax.persistence.EntityManager entityManager)
Executes this
SearchQuery with the given EntityManager . |
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. |
String |
getEntityAlias()
Gets the entity alias to by used in JPQL queries
|
SearchQuery<T> |
greaterEquals(String name,
Object value)
Checks if the attribute is greater or equals the given value.
JPQL-fragment: |
SearchQuery<T> |
greaterThan(String name,
Object value)
Checks if the attribute is greater than the given value.
JPQL-fragment: |
SearchQuery<T> |
in(String name,
Collection<?> values)
Checks if the attribute is in the given values.
JPQL-fragment: |
boolean |
isAppendEntityAlias()
Checks whether or not to add the entity alias
e. for each criteria in the JPQL query. |
SearchQuery<T> |
isNotNull(String name)
Checks if the attribute is not null.
JPQL-fragment: |
SearchQuery<T> |
isNull(String name)
Checks if the attribute is null.
JPQL-fragment: |
void |
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 |
SearchQuery<T> |
lessEquals(String name,
Object value)
Checks if the attribute is less or equals the given value.
JPQL-fragment: |
SearchQuery<T> |
lessThan(String name,
Object value)
Checks if the attribute is less than the given value.
JPQL-fragment: |
SearchQuery<T> |
like(String name,
Object value)
Checks if the attribute is like the given value.
JPQL-fragment: |
SearchQuery<T> |
notEquals(String name,
Object value)
Checks if the attribute not equals the given value.
JPQL-fragment: |
SearchQuery<T> |
notIn(String name,
Collection<?> values)
Checks if the attribute is not in the given values.
JPQL-fragment: |
SearchQuery<T> |
notLike(String name,
Object value)
Checks if the attribute is not like the given value.
JPQL-fragment: |
void |
setAppendEntityAlias(boolean appendEntityAlias)
Set to
false to avoid adding the entity alias e. for each criteria in the JPQL query. |
void |
setEntityAlias(String entityAlias)
Sets the entity alias to by used in JPQL queries
|
protected void |
setQueryParameters(javax.persistence.Query... queries)
Sets the parameters defined by
criteria for the given queries. |
SearchQuery<T> |
startsWith(String name,
Object value)
Checks if the attribute is starts with the given value.
JPQL-fragment: |
String |
toString() |
protected static final String WHERE
protected static final String AND
protected static final String PERCENT
protected static final String DOT
protected List<SearchQuery.Criterion> criteria
protected boolean distinct
protected String joinQuery
protected boolean appendEntityAlias
protected String entityAlias
protected List<SearchQuery.Clause> andClauses
public SearchQuery(Class<T> domainClass)
SearchQuery
for the given type.domainClass
- the type of the JPA Entity
public boolean isAppendEntityAlias()
e.
for each criteria in the JPQL query. Default is
true
.true
when entity alias gets appended, false
otherwisepublic void and(String clause, Map<String,Object> params)
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);
clause
- the AND clauseparams
- the named parameters for the AND-clauseand(String)
,
setAppendEntityAlias(boolean)
public void and(String clause)
SearchQuery query = repository.createSearchQuery(); query.and("(e.valid_from before now() or e.valid_from is null)");
clause
- the AND-clauseand(String, Map)
,
setAppendEntityAlias(boolean)
public void setAppendEntityAlias(boolean appendEntityAlias)
false
to avoid adding the entity alias e.
for each criteria in the JPQL query.appendEntityAlias
- isAppendEntityAlias()
,
setEntityAlias(String)
public String getEntityAlias()
public void setEntityAlias(String entityAlias)
entityAlias
- the aliaspublic final SearchQuery<T> equals(String name, Object value)
e.name = :value
name
- the name of the attributevalue
- the value to checkSearchQuery
public final SearchQuery<T> notEquals(String name, Object value)
e.name != :value
name
- the name of the attributevalue
- the value to checkSearchQuery
public final SearchQuery<T> greaterThan(String name, Object value)
e.name > :value
name
- the name of the attributevalue
- the value to checkSearchQuery
public final SearchQuery<T> greaterEquals(String name, Object value)
e.name >= :value
name
- the name of the attributevalue
- the value to checkSearchQuery
public final SearchQuery<T> lessThan(String name, Object value)
e.name < :value
name
- the name of the attributevalue
- the value to checkSearchQuery
public final SearchQuery<T> lessEquals(String name, Object value)
e.name <= :value
name
- the name of the attributevalue
- the value to checkSearchQuery
public final SearchQuery<T> in(String name, Collection<?> values)
e.name in :value
name
- the name of the attributevalues
- the values to checkSearchQuery
public final SearchQuery<T> notIn(String name, Collection<?> values)
e.name not in :value
name
- the name of the attributevalues
- the values to checkSearchQuery
public final SearchQuery<T> contains(String name, Object value)
e.name like :valueYou don't have to add the wildcard '
%
' before and after the value
, this is done
internally!name
- the name of the attributevalue
- the value to checkSearchQuery
public final SearchQuery<T> like(String name, Object value)
e.name like :valueThe
value
should contain some wildcards ('%
').name
- the name of the attributevalue
- the value to checkSearchQuery
startsWith(String, Object)
,
endsWith(String, Object)
public final SearchQuery<T> notLike(String name, Object value)
e.name not like :valueThe
value
should contain some wildcards ('%
').name
- the name of the attributevalue
- the value to checkSearchQuery
startsWith(String, Object)
,
endsWith(String, Object)
public final SearchQuery<T> startsWith(String name, Object value)
e.name like :valueYou don't have to add the wildcard '
%
' after the value
, this is done internally!name
- the name of the attributevalue
- the value to checkSearchQuery
like(String, Object)
public final SearchQuery<T> endsWith(String name, Object value)
e.name like :valueYou don't have to add the wildcard '
%
' before the value
, this is done internally!name
- the name of the attributevalue
- the value to checkSearchQuery
like(String, Object)
public final SearchQuery<T> isNotNull(String name)
e.name is not null
name
- the name of the attributeSearchQuery
public final SearchQuery<T> isNull(String name)
e.name is null
name
- the name of the attributeSearchQuery
protected void appendOrder(org.springframework.data.domain.Pageable pageable, StringBuilder queryBuilder, String entityName)
ORDER BY
clause, derived from the pageable
, to the queryBuilder
, using the
given entityName
.pageable
- the Pageable
, may be null
queryBuilder
- the query builderentityName
- the name for the entityprotected void appendOrder(org.springframework.data.domain.Sort sort, StringBuilder queryBuilder, String entityName)
ORDER BY
clause, derived from the sort
, to the queryBuilder
, using the given
entityName
.sort
- the Sort
, may be null
queryBuilder
- the query builderentityName
- the name for the entitypublic void join(String joinQuery)
e
, so you could add a join like
join e.addresses a
joinQuery
- the join-part of a JPQL querypublic void distinct()
distinct
entities only.public List<T> execute(javax.persistence.EntityManager entityManager)
SearchQuery
with the given EntityManager
.entityManager
- the EntityManager
public org.springframework.data.domain.Page<T> execute(org.springframework.data.domain.Pageable pageable, javax.persistence.EntityManager entityManager)
SearchQuery
with the given EntityManager
, applying the given Pageable
(if
any) for paging and sorting the results.pageable
- a Pageable
(optional)entityManager
- the EntityManager
Page
SearchRepository.search(SearchQuery, Pageable)
protected StringBuilder buildQueryString()
domainClass
entityAlias
and appendEntityAlias
joinQuery
criteria
andClauses
StringBuilder
containing the query string.join(String)
,
and(String)
,
and(String, Map)
,
setEntityAlias(String)
,
setAppendEntityAlias(boolean)
protected StringBuilder appendJoinAndWhereClause(StringBuilder queryBuilder)
queryBuilder
queryBuilder
- the StringBuilder
used to build the queryqueryBuilder
join(String)
protected void setQueryParameters(javax.persistence.Query... queries)
criteria
for the given queries.queries
- protected org.springframework.data.domain.Pageable applyPagination(javax.persistence.Query query, Long total, org.springframework.data.domain.Pageable pageable)
pageable
to the query. If the pageable's offset is >= total
, a new
Pageable
starting at page 0 is returned.query
- the Query
total
- the total number of itemspageable
- the Pageable
Pageable
Copyright © 2011–2020 aiticon GmbH. All rights reserved.