public class Query<T extends Model> extends Object
Query
class allows filtering and fetching records quickly.
It also provides update(Map)
and delete()
method to perform mass update and
delete operation on matched records.
Modifier and Type | Class and Description |
---|---|
class |
Query.Selector
A helper class to select specific field values.
|
Constructor and Description |
---|
Query(Class<T> beanClass)
Create a new instance of
Query with given bean class. |
Modifier and Type | Method and Description |
---|---|
Query<T> |
autoFlush(boolean auto) |
Query<T> |
bind(Map<String,Object> params)
Bind the named parameters of the query with the given values.
|
protected QueryBinder |
bind(Query query) |
Query<T> |
bind(String name,
Object value)
Bind the given named parameter of the query with the given value.
|
Query<T> |
cacheable()
Set the query result cacheable.
|
long |
count()
Returns the number of total records matched.
|
protected String |
countQuery() |
int |
delete()
Bulk delete all the matched records.
|
protected String |
deleteQuery(String filter) |
protected EntityManager |
em() |
List<T> |
fetch()
Fetch all the matched records.
|
List<T> |
fetch(int limit)
Fetch the matched records with the given limit.
|
List<T> |
fetch(int limit,
int offset)
Fetch the matched records within the given range.
|
T |
fetchOne()
Fetch the first matched record.
|
T |
fetchOne(int offset)
Fetch a matched record at the given offset.
|
java.util.stream.Stream<T> |
fetchSteam()
Deprecated.
Use
fetchStream() . |
java.util.stream.Stream<T> |
fetchSteam(int limit)
Deprecated.
Use
fetchStream(int) . |
java.util.stream.Stream<T> |
fetchSteam(int limit,
int offset)
Deprecated.
|
java.util.stream.Stream<T> |
fetchStream()
Fetch all the matched records as
Stream . |
java.util.stream.Stream<T> |
fetchStream(int limit)
Fetch the matched records as
Stream with the given limit. |
java.util.stream.Stream<T> |
fetchStream(int limit,
int offset)
Fetch the matched records as
Stream within the given range. |
Query<T> |
filter(String filter) |
Query<T> |
filter(String filter,
Object... params)
A convenient method to filter the query using JPQL's where clause.
|
static <T extends Model> |
of(Class<T> klass) |
Query<T> |
order(String spec)
Set order by clause for the query.
|
Query<T> |
readOnly()
Set the query readonly.
|
long |
remove()
Remove all the matched records.
|
Query.Selector |
select(String... names)
Return a selector to select records with specific fields only.
|
protected String |
selectQuery() |
protected String |
selectQuery(boolean update) |
String |
toString() |
int |
update(Map<String,Object> values)
Perform mass update on the matched records with the given values.
|
int |
update(Map<String,Object> values,
User updatedBy)
Perform mass update on matched records with the given values.
|
int |
update(String name,
Object value)
This is similar to
update(Map) but updates only single field. |
int |
update(String name,
Object value,
User updatedBy)
This is similar to
update(Map, User) but updates only single field. |
protected String |
updateQuery() |
protected String |
updateQuery(Map<String,Object> values,
boolean versioned,
String filter) |
protected EntityManager em()
public Query<T> filter(String filter, Object... params)
The filter string should refer the field names with self.
prefix and values should
not be embedded into the filter string but should be passed by parameters and ?
placeholder should be used to mark parameter substitutions.
Here is an example:
Query<Person> query = Query.of(Person); query = query.filter("self.name = ? AND self.age >= ?", "some", 20); List<Person> matched = query.fetch();
This is equivalent to:
SELECT self from Person self WHERE (self.name = ?1) AND (self.age >= ?2)
The params passed will be added as positional parameters to the JPA query object before
performing fetch()
.
filter
- the filter stringparams
- the parameterspublic Query<T> order(String spec)
The spec
is just a field name for ASC
or should be prefixed with -
for DESC
clause.
For example:
Query<Person> query = Query.of(Person); query = query.filter("name =", "some").filter("age >=", 20) .filter("lang in", "en", "hi"); query = query.order("name").order("-age");
This is equivalent to:
SELECT p from Person p WHERE (p.name = ?1) AND (p.age >= ?2) AND (lang IN (?3, ?4)) ORDER BY p.name, p.age DESC
spec
- order specpublic Query<T> cacheable()
public java.util.stream.Stream<T> fetchStream()
Stream
.
Recommended only when dealing with large data, for example, batch processing. For normal use
cases, the fetch()
is more appropriate.
Also configure hibernate.jdbc.fetch_size
(default is 20) to fine tune the fetch
size.
fetchStream(int)
,
fetchStream(int, int)
@Deprecated public java.util.stream.Stream<T> fetchSteam()
fetchStream()
.public java.util.stream.Stream<T> fetchStream(int limit)
Stream
with the given limit.
Recommended only when dealing with large data, for example, batch processing. For normal use
cases, the fetch()
is more appropriate.
Also configure hibernate.jdbc.fetch_size
(default is 20) to fine tune the fetch
size.
limit
- the limitfetchStream(int, int)
@Deprecated public java.util.stream.Stream<T> fetchSteam(int limit)
fetchStream(int)
.public java.util.stream.Stream<T> fetchStream(int limit, int offset)
Stream
within the given range.
Recommended only when dealing with large data, for example, batch processing. For normal use
cases, the fetch()
is more appropriate.
Also configure hibernate.jdbc.fetch_size
(default is 20) to fine tune the fetch
size.
limit
- the limitoffset
- the offset@Deprecated public java.util.stream.Stream<T> fetchSteam(int limit, int offset)
fetchStream(int, int)
.public List<T> fetch()
public List<T> fetch(int limit)
limit
- the limitpublic List<T> fetch(int limit, int offset)
limit
- the limitoffset
- the offsetpublic T fetchOne()
public T fetchOne(int offset)
offset
- the offsetpublic long count()
public Query.Selector select(String... names)
names
- field names to selectQuery.Selector
public int update(Map<String,Object> values)
values
- the key value mappublic int update(String name, Object value)
update(Map)
but updates only single field.name
- the field name whose value needs to be changedvalue
- the new valuepublic int update(Map<String,Object> values, User updatedBy)
If updatedBy
user is null, perform non-versioned update otherwise performed
versioned update.
values
- the key value mapupdatedBy
- the user to set "updatedBy" fieldpublic int update(String name, Object value, User updatedBy)
update(Map, User)
but updates only single field.name
- the field name whose value needs to be changedvalue
- the new valueupdatedBy
- the user to set "updatedBy" fieldpublic int delete()
DELETE
query and performs Query.executeUpdate()
.remove()
public long remove()
delete()
method, it performs EntityManager.remove(Object)
operation by fetching objects in pages (100 at a time).delete()
protected String selectQuery(boolean update)
protected String selectQuery()
protected String updateQuery()
protected String countQuery()
protected String updateQuery(Map<String,Object> values, boolean versioned, String filter)
protected QueryBinder bind(Query query)
public Query<T> bind(Map<String,Object> params)
params
- mapping for named params.public Query<T> bind(String name, Object value)
name
- the named parameter to bindvalue
- the parameter value