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)
.
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
.
SearchRepository.search(SearchQuery, Pageable)
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.
|
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. |
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. |
SearchQuery<T> |
startsWith(String name,
Object value)
Checks if the attribute is starts with the given value.
JPQL-fragment: |
String |
toString() |
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()
public 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
public 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 final List<T> execute(javax.persistence.EntityManager entityManager)
SearchQuery
with the given EntityManager
.entityManager
- the EntityManager
public final 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)
Copyright © 2011–2017 aiticon GmbH. All rights reserved.