Package org.openbravo.dal.service
Class OBQuery<E extends BaseOBObject>
- java.lang.Object
-
- org.openbravo.dal.service.OBQuery<E>
-
public class OBQuery<E extends BaseOBObject> extends Object
The OBQuery supports querying in the Data Access Layer with free-format (HQL) where and order by clauses. The OBQuery automatically adds applicable client and organization filters and handles joining of entities for orderby clauses.- Author:
- mtaal
- See Also:
OBCriteria
,OBDal
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
count()
Counts the number of objects in the database on the basis of the whereclause of the query.org.hibernate.query.Query<E>
createQuery()
Creates a Hibernate Query object using the whereclause and extra filters (for readable organizations etc.).<T> org.hibernate.query.Query<T>
createQuery(Class<T> clz)
Creates a Hibernate Query object using the whereclause and extra filters (for readable organizations etc.).org.hibernate.query.Query
deleteQuery()
Creates a Hibernate Query object intended to delete records of the Entity associated to the OBQuery instance.Entity
getEntity()
int
getFetchSize()
Returns the fetch size of the underlying query.int
getFirstResult()
Returns the position of the first row to be retrieved by the underlying query.int
getMaxResult()
Returns the maximum number of rows to be retrieved by the underlying query.Map<String,Object>
getNamedParameters()
The named parameters used in the query.String
getQueryType()
Returns the type of the underlying query.int
getRowNumber(String targetId)
Computes the row number of a record which has the id which is passed in as a parameter.String
getSelectClause()
Returns the select clause defined for the underlying query.String
getWhereAndOrderBy()
boolean
isFilterOnActive()
Controls if the isActive column is used as a filter (isActive == 'Y').boolean
isFilterOnReadableClients()
Filter the results on readable clients (@see OBContext#getReadableClients()).boolean
isFilterOnReadableOrganization()
Controls if the readable organizations should be used as a filter in the query.Iterator<E>
iterate()
Deprecated.List<E>
list()
Queries the database using the where clauses and additional active, client and organization filters.org.hibernate.ScrollableResults
scroll(org.hibernate.ScrollMode scrollMode)
Makes it possible to get aScrollableResults
from the underlying Query object.OBQuery<E>
setFetchSize(int fetchSize)
Sets a fetch size for the underlying query.OBQuery<E>
setFilterOnActive(boolean filterOnActive)
Controls if the isActive column is used as a filter (isActive == 'Y').OBQuery<E>
setFilterOnReadableClients(boolean filterOnReadableClients)
Filter the results on readable clients (@see OBContext#getReadableClients()).OBQuery<E>
setFilterOnReadableOrganization(boolean filterOnReadableOrganizations)
Controls if the readable organizations should be used as a filter in the query.OBQuery<E>
setFirstResult(int firstResult)
Sets the position of the first row to retrieve.OBQuery<E>
setMaxResult(int maxResult)
Sets the maximum number of rows to retrieve.OBQuery<E>
setNamedParameter(String paramName, Object value)
Sets one named parameter used in the query.OBQuery<E>
setNamedParameters(Map<String,Object> namedParameters)
Set the named parameters used in the query.void
setParameters(List<Object> parameters)
Deprecated.usesetNamedParameters(Map)
instead.OBQuery<E>
setQueryType(String queryType)
Sets the type of the underlying query.OBQuery<E>
setSelectClause(String selectClause)
Defines a select clause for the underlying query.OBQuery<E>
setWhereAndOrderBy(String queryString)
Sets the where and order by clause in the query.Stream<E>
stream()
Makes it possible to get aStream
over the underlying Query object.E
uniqueResult()
Queries the database using the where clauses and additional active, client and organization filters.Object
uniqueResultObject()
Queries the database using the where clauses and additional active, client and organization filters.
-
-
-
Method Detail
-
uniqueResult
public E uniqueResult()
Queries the database using the where clauses and additional active, client and organization filters.- Returns:
- single result or null
- Throws:
org.hibernate.HibernateException
- if the query returns more than one result- See Also:
uniqueResultObject for a version returning an Object
-
uniqueResultObject
public Object uniqueResultObject()
Queries the database using the where clauses and additional active, client and organization filters.- Returns:
- single result of type Object or null
- Throws:
org.hibernate.HibernateException
- if the query returns more than one result- See Also:
uniqueResult for a type-safe version
-
list
public List<E> list()
Queries the database using the where clauses and additional active, client and organization filters. The order in the list is determined by order by clause.- Returns:
- list of objects retrieved from the database
-
stream
public Stream<E> stream()
Makes it possible to get aStream
over the underlying Query object. Note that theBaseStream.close()
method should be invoked after processing the stream so that the underlying resources are deallocated right away.- Returns:
- a
Stream
over the underlying Query object.
-
iterate
@Deprecated public Iterator<E> iterate()
Deprecated.Queries the database using the where clauses and addition active, client and organization filters. The order in the list is determined by order by clause. Returns an iterator over the data.- Returns:
- iterator which walks over the list of objects in the db
-
scroll
public org.hibernate.ScrollableResults scroll(org.hibernate.ScrollMode scrollMode)
Makes it possible to get aScrollableResults
from the underlying Query object.- Parameters:
scrollMode
- the scroll mode to be used- Returns:
- the scrollable results which can be scrolled in the direction supported by the scrollMode
-
count
public int count()
Counts the number of objects in the database on the basis of the whereclause of the query.- Returns:
- the number of objects in the database taking into account the where and orderby clause
-
getRowNumber
public int getRowNumber(String targetId)
Computes the row number of a record which has the id which is passed in as a parameter. The rownumber computation takes into account the filter and sorting settings of the the OBQuery object.- Parameters:
targetId
- the record id- Returns:
- the row number or -1 if not found
-
deleteQuery
public org.hibernate.query.Query deleteQuery()
Creates a Hibernate Query object intended to delete records of the Entity associated to the OBQuery instance. To generate the criteria of the deletion, it makes use of the whereclause and extra filters (for readable organizations etc.).- Returns:
- a new Hibernate Query object. Note that it does not have an specific type because delete queries can not be typed.
-
createQuery
public org.hibernate.query.Query<E> createQuery()
Creates a Hibernate Query object using the whereclause and extra filters (for readable organizations etc.).- Returns:
- a new Hibernate Query object
-
createQuery
public <T> org.hibernate.query.Query<T> createQuery(Class<T> clz)
Creates a Hibernate Query object using the whereclause and extra filters (for readable organizations etc.). The Query will return objects with the type specified as parameter (unless a specific select clause is provided using thesetSelectClause(String)
method).- Parameters:
clz
- the class of the query's resulting objects- Returns:
- a new Hibernate Query object
-
getEntity
public Entity getEntity()
- Returns:
- the Entity queried by the Query object
-
isFilterOnReadableOrganization
public boolean isFilterOnReadableOrganization()
Controls if the readable organizations should be used as a filter in the query. The default is true.- Returns:
- if false then readable organizations are not added as a filter to the query
-
setFilterOnReadableOrganization
public OBQuery<E> setFilterOnReadableOrganization(boolean filterOnReadableOrganizations)
Controls if the readable organizations should be used as a filter in the query. The default is true.- Parameters:
filterOnReadableOrganizations
- if set to false then readable organizations are not added as a filter to the query- Returns:
- this OBQuery instance, for method chaining.
-
isFilterOnActive
public boolean isFilterOnActive()
Controls if the isActive column is used as a filter (isActive == 'Y'). The default is true.- Returns:
- if false then isActive is not used as a filter for the query
-
setFilterOnActive
public OBQuery<E> setFilterOnActive(boolean filterOnActive)
Controls if the isActive column is used as a filter (isActive == 'Y'). The default is true.- Parameters:
filterOnActive
- if false then isActive is not used as a filter for the query, if true (the default) then isActive='Y' is added as a filter to the query- Returns:
- this OBQuery instance, for method chaining.
-
getWhereAndOrderBy
public String getWhereAndOrderBy()
- Returns:
- the where and order by clause used in the query
-
setWhereAndOrderBy
public OBQuery<E> setWhereAndOrderBy(String queryString)
Sets the where and order by clause in the query.- Parameters:
queryString
- the where and order by parts of the query- Returns:
- this OBQuery instance, for method chaining.
-
setParameters
@Deprecated public void setParameters(List<Object> parameters)
Deprecated.usesetNamedParameters(Map)
instead.Set the non-named parameters ('?') in the query by converting them to named parameters. This conversion is done because legacy-style query parameters are no longer supported in Hibernate. Note that this method also parses the where and order by clauses of the query to make use of the newly generated named parameters.- Parameters:
parameters
- the parameters which are set in the query without a name (e.g. as :?)
-
isFilterOnReadableClients
public boolean isFilterOnReadableClients()
Filter the results on readable clients (@see OBContext#getReadableClients()). The default is true.- Returns:
- if true then only objects from readable clients are returned, if false then objects from all clients are returned
-
setFilterOnReadableClients
public OBQuery<E> setFilterOnReadableClients(boolean filterOnReadableClients)
Filter the results on readable clients (@see OBContext#getReadableClients()). The default is true.- Parameters:
filterOnReadableClients
- if true then only objects from readable clients are returned by this Query, if false then objects from all clients are returned- Returns:
- this OBQuery instance, for method chaining.
-
getNamedParameters
public Map<String,Object> getNamedParameters()
The named parameters used in the query.- Returns:
- the map of named parameters which are being used in the query
-
setNamedParameters
public OBQuery<E> setNamedParameters(Map<String,Object> namedParameters)
Set the named parameters used in the query.- Parameters:
namedParameters
- the list of named parameters (string, value pair)- Returns:
- this OBQuery instance, for method chaining.
-
setNamedParameter
public OBQuery<E> setNamedParameter(String paramName, Object value)
Sets one named parameter used in the query.- Parameters:
paramName
- name of the parametervalue
- value which should be used for this parameter- Returns:
- this OBQuery instance, for method chaining.
-
getFirstResult
public int getFirstResult()
Returns the position of the first row to be retrieved by the underlying query.- Returns:
- the position of the first row to be retrieved
-
setFirstResult
public OBQuery<E> setFirstResult(int firstResult)
Sets the position of the first row to retrieve.- Parameters:
firstResult
- the position of the first row to retrieve- Returns:
- this OBQuery instance, for method chaining.
-
getMaxResult
public int getMaxResult()
Returns the maximum number of rows to be retrieved by the underlying query.- Returns:
- the maximum number of rows to be retrieved
-
setMaxResult
public OBQuery<E> setMaxResult(int maxResult)
Sets the maximum number of rows to retrieve.- Parameters:
maxResult
- the maximum number of rows to retrieve- Returns:
- this OBQuery instance, for method chaining.
-
getFetchSize
public int getFetchSize()
Returns the fetch size of the underlying query.- Returns:
- the fetch size of the underlying query
-
setFetchSize
public OBQuery<E> setFetchSize(int fetchSize)
Sets a fetch size for the underlying query.- Parameters:
fetchSize
- the fetch size for the underlying query- Returns:
- this OBQuery instance, for method chaining.
-
getSelectClause
public String getSelectClause()
Returns the select clause defined for the underlying query.- Returns:
- the select clause defined for the underlying query
-
setSelectClause
public OBQuery<E> setSelectClause(String selectClause)
Defines a select clause for the underlying query. Important Note: this method can change the type of the object returned by the query, which is previously defined when instantiating the OBQuery object.- Parameters:
selectClause
- the select clause to be used by the underlying query.- Returns:
- this OBQuery instance, for method chaining.
-
setQueryType
public OBQuery<E> setQueryType(String queryType)
Sets the type of the underlying query.- Parameters:
queryType
- the type of the underlying query- Returns:
- this OBQuery instance, for method chaining.
-
getQueryType
public String getQueryType()
Returns the type of the underlying query.- Returns:
- a String with the type of the underlying query
-
-