Class EntityQuery

java.lang.Object
org.apache.ofbiz.entity.util.EntityQuery

public class EntityQuery extends Object
Used to setup various options for and subsequently execute entity queries. All methods to set options modify the EntityQuery instance then return this modified object to allow method call chaining. It is important to note that this object is not immutable and is modified internally, and returning EntityQuery is just a self reference for convenience. After a query the object can be further modified and then used to perform another query if desired.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct an EntityQuery object for use against the specified Delegator
  • Method Summary

    Modifier and Type
    Method
    Description
    Specifies whether results should be read from the cache (or written to the cache if the results have not yet been cached)
    cache(boolean useCache)
    Specifies whether results should be read from the cache (or written to the cache if the results have not yet been cached)
    Indicate that the ResultSet object's cursor may move only forward (this is the default behavior)
    Indicate that the ResultSet object's cursor is scrollable but generally not sensitive to changes to the data that underlies the ResultSet.
    Indicate that the ResultSet object's cursor is scrollable but generally sensitive to changes to the data that underlies the ResultSet.
    Specifies that the values returned should be filtered to remove duplicate values.
    distinct(boolean distinct)
    Specifies whether the values returned should be filtered to remove duplicate values.
    fetchSize(int fetchSize)
    Specifies the fetch size for this query.
    Specifies whether the query should return only values that are currently active using from/thruDate fields.
    filterByDate(String... filterByFieldName)
    Specifies whether the query should return only values that are currently active using the specified from/thru field name pairs.
    Specifies whether the query should return only values that are active during the specified moment using from/thruDate fields.
    filterByDate(Timestamp moment, String... filterByFieldName)
    Specifies whether the query should return only values that are active during the specified moment using the specified from/thru field name pairs.
    Specifies whether the query should return only values that are active during the specified moment using from/thruDate fields.
    from(String entityName)
    Set the entity to query against
    from(DynamicViewEntity dynamicViewEntity)
    Set the entity to query against
    <T> List<T>
    getFieldList(String fieldName)
    Gets a list of values (no matter which type) for a specified entity field name.
    Gets limit.
    Gets offset.
    having(EntityCondition entityCondition)
    Set the EntityCondition to be used as the HAVING clause for the query.
    limit(int limit)
    Limit entity query.
    maxRows(int maxRows)
    Specifies the max number of rows to return, 0 means all rows.
    offset(int offset)
    Offset entity query.
    orderBy(String... fields)
    The fields of the named entity to order the resultset by; optionally add a " ASC" for ascending or " DESC" for descending NOTE: Each successive call to any of the orderBy(...) methods will replace the currently set orderBy fields for the query.
    orderBy(List<String> orderBy)
    The fields of the named entity to order the resultset by; optionally add a " ASC" for ascending or " DESC" for descending NOTE: Each successive call to any of the orderBy(...) methods will replace the currently set orderBy fields for the query.
    query batch iterator
    long
    Executes the EntityQuery and returns the result count If the query generates more than a single result then an exception is thrown
    Executes the EntityQuery and returns the first result
    Executes the EntityQuery and returns an EntityListIterator representing the result of the query.
    Executes the EntityQuery and returns a list of results
    Executes the EntityQuery and a single result record
    queryPagedList(int viewIndex, int viewSize)
     
    select(String... fields)
    Set the fields to be returned when the query is executed.
    select(Set<String> fieldsToSelect)
    Set the fields to be returned when the query is executed.
    use(Delegator delegator)
    Construct an EntityQuery object for use against the specified Delegator
    where(Object... fields)
    Set a series of field name/values to be ANDed together as the WHERE clause for the query NOTE: Each successive call to any of the where(...) methods will replace the currently set condition for the query.
    where(List<T> andConditions)
    Set a list of EntityCondition objects to be ANDed together as the WHERE clause for the query NOTE: Each successive call to any of the where(...) methods will replace the currently set condition for the query.
    where(Map<String,Object> fieldMap)
    Set a Map of field name/values to be ANDed together as the WHERE clause for the query NOTE: Each successive call to any of the where(...) methods will replace the currently set condition for the query.
    where(EntityCondition entityCondition)
    Set the EntityCondition to be used as the WHERE clause for the query NOTE: Each successive call to any of the where(...) methods will replace the currently set condition for the query.
    where(EntityCondition... entityCondition)
    Set a series of EntityConditions to be ANDed together as the WHERE clause for the query NOTE: Each successive call to any of the where(...) methods will replace the currently set condition for the query.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • EntityQuery

      public EntityQuery(Delegator delegator)
      Construct an EntityQuery object for use against the specified Delegator
      Parameters:
      delegator - The delegator instance to use for the query
  • Method Details

    • use

      public static EntityQuery use(Delegator delegator)
      Construct an EntityQuery object for use against the specified Delegator
      Parameters:
      delegator - The delegator instance to use for the query
    • select

      public EntityQuery select(Set<String> fieldsToSelect)
      Set the fields to be returned when the query is executed. Note that the select methods are not additive, if a subsequent call is made to select then the existing fields for selection will be replaced.
      Parameters:
      fieldsToSelect - - A Set of Strings containing the field names to be selected
      Returns:
      this EntityQuery object, to enable chaining
    • select

      public EntityQuery select(String... fields)
      Set the fields to be returned when the query is executed. Note that the select methods are not additive, if a subsequent call is made to select then the existing fields for selection will be replaced.
      Parameters:
      fields - - Strings containing the field names to be selected
      Returns:
      this EntityQuery object, to enable chaining
    • from

      public EntityQuery from(String entityName)
      Set the entity to query against
      Parameters:
      entityName - - The name of the entity to query against
      Returns:
      this EntityQuery object, to enable chaining
    • from

      public EntityQuery from(DynamicViewEntity dynamicViewEntity)
      Set the entity to query against
      Parameters:
      dynamicViewEntity - - The DynamicViewEntity object to query against
      Returns:
      this EntityQuery object, to enable chaining
    • where

      public EntityQuery where(EntityCondition entityCondition)
      Set the EntityCondition to be used as the WHERE clause for the query NOTE: Each successive call to any of the where(...) methods will replace the currently set condition for the query.
      Parameters:
      entityCondition - - An EntityCondition object to be used as the where clause for this query
      Returns:
      this EntityQuery object, to enable chaining
    • where

      public EntityQuery where(Map<String,Object> fieldMap)
      Set a Map of field name/values to be ANDed together as the WHERE clause for the query NOTE: Each successive call to any of the where(...) methods will replace the currently set condition for the query.
      Parameters:
      fieldMap - - A Map of field names/values to be ANDed together as the where clause for the query
      Returns:
      this EntityQuery object, to enable chaining
    • where

      public EntityQuery where(Object... fields)
      Set a series of field name/values to be ANDed together as the WHERE clause for the query NOTE: Each successive call to any of the where(...) methods will replace the currently set condition for the query.
      Parameters:
      fields - - A series of field names/values to be ANDed together as the where clause for the query
      Returns:
      this EntityQuery object, to enable chaining
    • where

      public EntityQuery where(EntityCondition... entityCondition)
      Set a series of EntityConditions to be ANDed together as the WHERE clause for the query NOTE: Each successive call to any of the where(...) methods will replace the currently set condition for the query.
      Parameters:
      entityCondition - - A series of EntityConditions to be ANDed together as the where clause for the query
      Returns:
      this EntityQuery object, to enable chaining
    • where

      public <T extends EntityCondition> EntityQuery where(List<T> andConditions)
      Set a list of EntityCondition objects to be ANDed together as the WHERE clause for the query NOTE: Each successive call to any of the where(...) methods will replace the currently set condition for the query.
      Parameters:
      andConditions - - A list of EntityCondition objects to be ANDed together as the WHERE clause for the query
      Returns:
      this EntityQuery object, to enable chaining
    • having

      public EntityQuery having(EntityCondition entityCondition)
      Set the EntityCondition to be used as the HAVING clause for the query. NOTE: Each successive call to any of the having(...) methods will replace the currently set condition for the query.
      Parameters:
      entityCondition - - The EntityCondition object that specifies how to constrain this query after any groupings are done (if this is a view entity with group-by aliases)
      Returns:
      this EntityQuery object, to enable chaining
    • orderBy

      public EntityQuery orderBy(List<String> orderBy)
      The fields of the named entity to order the resultset by; optionally add a " ASC" for ascending or " DESC" for descending NOTE: Each successive call to any of the orderBy(...) methods will replace the currently set orderBy fields for the query.
      Parameters:
      orderBy - - The fields of the named entity to order the resultset by
      Returns:
      this EntityQuery object, to enable chaining
    • orderBy

      public EntityQuery orderBy(String... fields)
      The fields of the named entity to order the resultset by; optionally add a " ASC" for ascending or " DESC" for descending NOTE: Each successive call to any of the orderBy(...) methods will replace the currently set orderBy fields for the query.
      Parameters:
      fields - - The fields of the named entity to order the resultset by
      Returns:
      this EntityQuery object, to enable chaining
    • cursorForwardOnly

      public EntityQuery cursorForwardOnly()
      Indicate that the ResultSet object's cursor may move only forward (this is the default behavior)
      Returns:
      this EntityQuery object, to enable chaining
    • cursorScrollSensitive

      public EntityQuery cursorScrollSensitive()
      Indicate that the ResultSet object's cursor is scrollable but generally sensitive to changes to the data that underlies the ResultSet.
      Returns:
      this EntityQuery object, to enable chaining
    • cursorScrollInsensitive

      public EntityQuery cursorScrollInsensitive()
      Indicate that the ResultSet object's cursor is scrollable but generally not sensitive to changes to the data that underlies the ResultSet.
      Returns:
      this EntityQuery object, to enable chaining
    • fetchSize

      public EntityQuery fetchSize(int fetchSize)
      Specifies the fetch size for this query. -1 will fall back to datasource settings.
      Parameters:
      fetchSize - - The fetch size for this query
      Returns:
      this EntityQuery object, to enable chaining
    • maxRows

      public EntityQuery maxRows(int maxRows)
      Specifies the max number of rows to return, 0 means all rows.
      Parameters:
      maxRows - - the max number of rows to return
      Returns:
      this EntityQuery object, to enable chaining
    • offset

      public EntityQuery offset(int offset)
      Offset entity query.
      Parameters:
      offset - the offset
      Returns:
      the entity query
    • getOffset

      public Integer getOffset()
      Gets offset.
      Returns:
      the offset
    • limit

      public EntityQuery limit(int limit)
      Limit entity query.
      Parameters:
      limit - the limit
      Returns:
      the entity query
    • getLimit

      public Integer getLimit()
      Gets limit.
      Returns:
      the limit
    • distinct

      public EntityQuery distinct()
      Specifies that the values returned should be filtered to remove duplicate values.
      Returns:
      this EntityQuery object, to enable chaining
    • distinct

      public EntityQuery distinct(boolean distinct)
      Specifies whether the values returned should be filtered to remove duplicate values.
      Parameters:
      distinct - - boolean indicating whether the values returned should be filtered to remove duplicate values
      Returns:
      this EntityQuery object, to enable chaining
    • cache

      public EntityQuery cache()
      Specifies whether results should be read from the cache (or written to the cache if the results have not yet been cached)
      Returns:
      this EntityQuery object, to enable chaining
    • cache

      public EntityQuery cache(boolean useCache)
      Specifies whether results should be read from the cache (or written to the cache if the results have not yet been cached)
      Parameters:
      useCache - - boolean to indicate if the cache should be used or not
      Returns:
      this EntityQuery object, to enable chaining
    • filterByDate

      public EntityQuery filterByDate()
      Specifies whether the query should return only values that are currently active using from/thruDate fields.
      Returns:
      this EntityQuery object, to enable chaining
    • filterByDate

      public EntityQuery filterByDate(Timestamp moment)
      Specifies whether the query should return only values that are active during the specified moment using from/thruDate fields.
      Parameters:
      moment - - Timestamp representing the moment in time that the values should be active during
      Returns:
      this EntityQuery object, to enable chaining
    • filterByDate

      public EntityQuery filterByDate(Date moment)
      Specifies whether the query should return only values that are active during the specified moment using from/thruDate fields.
      Parameters:
      moment - - Date representing the moment in time that the values should be active during
      Returns:
      this EntityQuery object, to enable chaining
    • filterByDate

      public EntityQuery filterByDate(String... filterByFieldName)
      Specifies whether the query should return only values that are currently active using the specified from/thru field name pairs.
      Parameters:
      filterByFieldName - - String pairs representing the from/thru date field names e.g. "fromDate", "thruDate", "contactFromDate", "contactThruDate"
      Returns:
      this EntityQuery object, to enable chaining
    • filterByDate

      public EntityQuery filterByDate(Timestamp moment, String... filterByFieldName)
      Specifies whether the query should return only values that are active during the specified moment using the specified from/thru field name pairs.
      Parameters:
      moment - - Timestamp representing the moment in time that the values should be active during
      filterByFieldName - - String pairs representing the from/thru date field names e.g. "fromDate", "thruDate", "contactFromDate", "contactThruDate"
      Returns:
      this EntityQuery object, to enable chaining
    • queryList

      public List<GenericValue> queryList() throws GenericEntityException
      Executes the EntityQuery and returns a list of results
      Returns:
      Returns a List of GenericValues representing the results of the query
      Throws:
      GenericEntityException
    • queryIterator

      public EntityListIterator queryIterator() throws GenericEntityException
      Executes the EntityQuery and returns an EntityListIterator representing the result of the query. NOTE: THAT THIS MUST BE CLOSED (preferably in a finally block) WHEN YOU ARE DONE WITH IT, AND DON'T LEAVE IT OPEN TOO LONG BEACUSE IT WILL MAINTAIN A DATABASE CONNECTION.
      Returns:
      Returns an EntityListIterator representing the result of the query
      Throws:
      GenericEntityException
    • queryBatchIterator

      public EntityBatchIterator queryBatchIterator()
      query batch iterator
    • queryFirst

      public GenericValue queryFirst() throws GenericEntityException
      Executes the EntityQuery and returns the first result
      Returns:
      GenericValue representing the first result record from the query
      Throws:
      GenericEntityException
    • queryOne

      public GenericValue queryOne() throws GenericEntityException
      Executes the EntityQuery and a single result record
      Returns:
      GenericValue representing the only result record from the query
      Throws:
      GenericEntityException
    • queryCount

      public long queryCount() throws GenericEntityException
      Executes the EntityQuery and returns the result count If the query generates more than a single result then an exception is thrown
      Returns:
      GenericValue representing the only result record from the query
      Throws:
      GenericEntityException
    • getFieldList

      public <T> List<T> getFieldList(String fieldName) throws GenericEntityException
      Gets a list of values (no matter which type) for a specified entity field name.

      The field of the entity is first selected and the cache usage turned off to ensure no values are missing.

      Type Parameters:
      T -
      Parameters:
      fieldName -
      Returns:
      list with field values
      Throws:
      GenericEntityException
    • queryPagedList

      public PagedList<GenericValue> queryPagedList(int viewIndex, int viewSize) throws GenericEntityException
      Parameters:
      viewIndex -
      viewSize -
      Returns:
      PagedList object with a subset of data items
      Throws:
      GenericEntityException
      See Also: