Class Query<T extends Model>
- java.lang.Object
-
- com.axelor.db.Query<T>
-
- Direct Known Subclasses:
MetaJsonRecordRepository.MetaJsonRecordQuery
public class Query<T extends Model> extends Object
TheQuery
class allows filtering and fetching records quickly.It also provides
update(Map)
anddelete()
method to perform mass update and delete operation on matched records.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
Query.JoinHelper
JoinHelper class is used to auto generateLEFT JOIN
for association expressions.class
Query.Selector
A helper class to select specific field values.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Query<T>
autoFlush(boolean auto)
Query<T>
bind(String name, Object value)
Bind the given named parameter of the query with the given value.Query<T>
bind(Map<String,Object> params)
Bind the named parameters of the query with the given values.protected QueryBinder
bind(javax.persistence.Query query)
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 javax.persistence.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.Stream<T>
fetchStream()
Fetch all the matched records asStream
.Stream<T>
fetchStream(int limit)
Fetch the matched records asStream
with the given limit.Stream<T>
fetchStream(int limit, int offset)
Fetch the matched records asStream
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.protected String
fixPlaceholders(String filter)
protected String
getFilter()
protected Query.JoinHelper
getJoinHelper()
protected Map<String,Object>
getNamedParams()
protected Object[]
getParams()
static <T extends Model>
Query<T>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)
protected void
setFilter(String filter)
protected void
setNamedParams(Map<String,Object> namedParams)
protected void
setParams(Object[] params)
String
toString()
Query<T>
translate()
Use translation join.Query<T>
translate(boolean translate)
Set whether to use translation join.int
update(String name, Object value)
This is similar toupdate(Map)
but updates only single field.int
update(String name, Object value, User updatedBy)
This is similar toupdate(Map, User)
but updates only single field.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.protected String
updateQuery()
protected String
updateQuery(Map<String,Object> values, boolean versioned, String filter)
-
-
-
Method Detail
-
em
protected javax.persistence.EntityManager em()
-
getJoinHelper
protected Query.JoinHelper getJoinHelper()
-
getFilter
protected String getFilter()
-
setFilter
protected void setFilter(String filter)
-
getParams
protected Object[] getParams()
-
setParams
protected void setParams(Object[] params)
-
filter
public Query<T> filter(String filter, Object... params)
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
-
order
public Query<T> order(String spec)
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
public Query<T> cacheable()
Set the query result cacheable.- Returns:
- the same query instance
-
translate
public Query<T> translate(boolean translate)
Set whether to use translation join.- Parameters:
translate
-- Returns:
-
fetchStream
public Stream<T> 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(int)
,fetchStream(int, int)
-
fetchStream
public Stream<T> fetchStream(int limit)
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(int, int)
-
fetchStream
public Stream<T> fetchStream(int limit, int offset)
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
public List<T> fetch()
Fetch all the matched records.- Returns:
- list of all the matched records.
-
fetch
public List<T> fetch(int limit)
Fetch the matched records with the given limit.- Parameters:
limit
- the limit- Returns:
- matched records within the limit
-
fetch
public List<T> fetch(int limit, int offset)
Fetch the matched records within the given range.- Parameters:
limit
- the limitoffset
- the offset- Returns:
- list of matched records within the range
-
fetchOne
public T fetchOne()
Fetch the first matched record.- Returns:
- the first matched record.
-
fetchOne
public T fetchOne(int offset)
Fetch a matched record at the given offset.- Parameters:
offset
- the offset- Returns:
- the matched record at given offset
-
count
public long count()
Returns the number of total records matched.- Returns:
- total number
-
select
public Query.Selector select(String... names)
Return a selector to select records with specific fields only.- Parameters:
names
- field names to select- Returns:
- a new instance of
Query.Selector
-
update
public int update(Map<String,Object> values)
Perform mass update on the matched records with the given values.- Parameters:
values
- the key value map- Returns:
- total number of records updated
-
update
public int update(String name, Object value)
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
public int update(Map<String,Object> values, User updatedBy)
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
public int update(String name, Object value, User updatedBy)
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()
-
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:
delete()
-
selectQuery
protected String selectQuery(boolean update)
-
selectQuery
protected String selectQuery()
-
updateQuery
protected String updateQuery()
-
countQuery
protected String countQuery()
-
updateQuery
protected String updateQuery(Map<String,Object> values, boolean versioned, String filter)
-
bind
protected QueryBinder bind(javax.persistence.Query query)
-
bind
public Query<T> bind(Map<String,Object> params)
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
public Query<T> bind(String name, Object value)
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
-
-