Class Query<T extends Model>
- Direct Known Subclasses:
MetaJsonRecordRepository.MetaJsonRecordQuery
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
JoinHelper class is used to auto generateLEFT JOIN
for association expressions.class
A helper class to select specific field values. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionautoFlush
(boolean auto) protected QueryBinder
bind
(jakarta.persistence.Query query) Bind the given named parameter of the query with the given value.Bind the named parameters of the query with the given values.Set the query result cacheable.long
count()
Returns the number of total records matched.protected String
int
delete()
Bulk delete all the matched records.protected String
deleteQuery
(String filter) protected jakarta.persistence.EntityManager
em()
fetch()
Fetch all the matched records.fetch
(int limit) Fetch the matched records with the given limit.fetch
(int limit, int offset) Fetch the matched records within the given range.fetchOne()
Fetch the first matched record.fetchOne
(int offset) Fetch a matched record at the given offset.Fetch all the matched records asStream
.fetchStream
(int limit) Fetch the matched records asStream
with the given limit.fetchStream
(int limit, int offset) Fetch the matched records asStream
within the given range.A convenient method to filter the query using JPQL's where clause.protected String
fixPlaceholders
(String filter) protected String
protected Query.JoinHelper
protected Object[]
Set order by clause for the query.readOnly()
Set the query readonly.long
remove()
Remove all the matched records.Return a selector to select records with specific fields only.protected String
protected String
selectQuery
(boolean update) protected void
protected void
setNamedParams
(Map<String, Object> namedParams) protected void
toString()
Use translation join.translate
(boolean translate) Set whether to use translation join.int
This is similar toupdate(Map)
but updates only single field.int
This is similar toupdate(Map, User)
but updates only single field.int
Perform mass update on the matched records with the given values.int
Perform mass update on matched records with the given values.protected String
protected String
updateQuery
(Map<String, Object> values, boolean versioned, String filter)
-
Constructor Details
-
Query
Create a new instance ofQuery
with given bean class.- Parameters:
beanClass
- model bean class
-
-
Method Details
-
of
-
em
protected jakarta.persistence.EntityManager em() -
getJoinHelper
-
getFilter
-
setFilter
-
getParams
-
setParams
-
getNamedParams
-
setNamedParams
-
filter
A convenient method to filter the query using JPQL's where clause.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()
.- Parameters:
filter
- the filter stringparams
- the parameters- Returns:
- the same instance
-
filter
-
fixPlaceholders
-
order
Set order by clause for the query. This method can be chained to provide multiple fields.The
spec
is just a field name forASC
or should be prefixed with-
forDESC
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
- Parameters:
spec
- order spec- Returns:
- the same query instance
-
cacheable
Set the query result cacheable.- Returns:
- the same query instance
-
readOnly
Set the query readonly.- Returns:
- the same query instance.
-
translate
Set whether to use translation join.- Parameters:
translate
-- Returns:
-
translate
Use translation join.- Returns:
- the same query instance.
-
autoFlush
-
fetchStream
Fetch all the matched records asStream
.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.- Returns:
- stream of matched records.
- See Also:
-
fetchStream
Fetch the matched records asStream
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.- Parameters:
limit
- the limit- Returns:
- stream of matched records within the limit
- See Also:
-
fetchStream
Fetch the matched records asStream
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.- Parameters:
limit
- the limitoffset
- the offset- Returns:
- stream of matched records within the range
-
fetch
Fetch all the matched records.- Returns:
- list of all the matched records.
-
fetch
Fetch the matched records with the given limit.- Parameters:
limit
- the limit- Returns:
- matched records within the limit
-
fetch
Fetch the matched records within the given range.- Parameters:
limit
- the limitoffset
- the offset- Returns:
- list of matched records within the range
-
fetchOne
Fetch the first matched record.- Returns:
- the first matched record, or null if there are no results.
-
fetchOne
Fetch a matched record at the given offset.- Parameters:
offset
- the offset- Returns:
- the matched record at given offset, or null if there are no results.
-
count
public long count()Returns the number of total records matched.- Returns:
- total number
-
select
Return a selector to select records with specific fields only. -
update
Perform mass update on the matched records with the given values.- Parameters:
values
- the key value map- Returns:
- total number of records updated
-
update
This is similar toupdate(Map)
but updates only single field.- Parameters:
name
- the field name whose value needs to be changedvalue
- the new value- Returns:
- total number of records updated
-
update
Perform mass update on matched records with the given values.If
updatedBy
user is null, perform non-versioned update otherwise performed versioned update.- Parameters:
values
- the key value mapupdatedBy
- the user to set "updatedBy" field- Returns:
- total number of records updated
-
update
This is similar toupdate(Map, User)
but updates only single field.- Parameters:
name
- the field name whose value needs to be changedvalue
- the new valueupdatedBy
- the user to set "updatedBy" field- Returns:
- total number of records updated
-
delete
public int delete()Bulk delete all the matched records.
This method usesDELETE
query and performsQuery.executeUpdate()
.- Returns:
- total number of records affected.
- See Also:
-
remove
public long remove()Remove all the matched records.
In contrast to thedelete()
method, it performsEntityManager.remove(Object)
operation by fetching objects in pages (100 at a time).- Returns:
- total number of records removed.
- See Also:
-
selectQuery
-
selectQuery
-
updateQuery
-
countQuery
-
updateQuery
-
deleteQuery
-
bind
-
bind
Bind the named parameters of the query with the given values. Named parameter must me set after query is filtered.- Parameters:
params
- mapping for named params.- Returns:
- the same instance
-
bind
Bind the given named parameter of the query with the given value.- Parameters:
name
- the named parameter to bindvalue
- the parameter value- Returns:
- the same instance
-
toString
-