Package org.apache.cayenne.query

## Class ObjectSelect<T>

- java.lang.Object
- - org.apache.cayenne.query.IndirectQuery
  - - org.apache.cayenne.query.FluentSelect<T>
    - - org.apache.cayenne.query.ObjectSelect<T>

- All Implemented Interfaces:
    `Serializable`, `Query`, `Select<T>`

      public class ObjectSelect<T>
      extends FluentSelect<T>

  A selecting query providing chainable API. This is an alternative to `SelectQuery` when you want to use a fluent API. For example, the following is a convenient way to return a record:

       Artist a = ObjectSelect
            .query(Artist.class)
            .where(Artist.NAME.eq("Picasso"))
            .selectOne(context);

  Since:
    4.0
  See Also:
    Serialized Form

- - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `protected boolean` | `fetchingDataRows` |

    - ### Fields inherited from class org.apache.cayenne.query.FluentSelect

      `cacheGroup, cacheStrategy, dbEntityName, entityName, entityType, limit, offset, orderings, pageSize, prefetches, statementFetchSize, where`

    - ### Fields inherited from class org.apache.cayenne.query.IndirectQuery

      `dataMap, lastResolver, name, replacementQuery`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor | Description
    --- | --- | ---
    `protected` | `ObjectSelect()` |

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `ObjectSelect<T>` | `and​(Collection<Expression> expressions)` | AND's provided expressions to the existing WHERE clause expression.
    `ObjectSelect<T>` | `and​(Expression... expressions)` | AND's provided expressions to the existing WHERE clause expression.
    `<E> ColumnSelect<E>` | `avg​(Property<E> property)` | Select average value of property
    `ObjectSelect<T>` | `cacheGroup​(String cacheGroup)` |
    `ObjectSelect<T>` | `cacheStrategy​(QueryCacheStrategy strategy)` |
    `ObjectSelect<T>` | `cacheStrategy​(QueryCacheStrategy strategy, String cacheGroup)` |
    `<E> ColumnSelect<E>` | `column​(Property<E> property)` | Select one specific property.
    `static ColumnSelect<Object[]>` | `columnQuery​(Class<?> entityType, Property<?> firstColumn, Property<?>... otherColumns)` | Creates a ColumnSelect that will fetch multiple columns of a given `ObjEntity`
    `static <E> ColumnSelect<E>` | `columnQuery​(Class<?> entityType, Property<E> column)` | Creates a ColumnSelect that will fetch single property that can be resolved against a given `ObjEntity` class.
    `ColumnSelect<Object[]>` | `columns​(Property<?> firstProperty, Property<?>... properties)` | Select only specific properties.
    `ColumnSelect<Long>` | `count()` | Select COUNT(*)
    `ColumnSelect<Long>` | `count​(Property<?> property)` | Select COUNT(property)
    `protected Query` | `createReplacementQuery​(EntityResolver resolver)` | Translates self to a SelectQuery.
    `static ObjectSelect<DataRow>` | `dataRowQuery​(Class<?> entityType)` | Creates a ObjectSelect that fetches data for an `ObjEntity` determined from a provided class.
    `static ObjectSelect<DataRow>` | `dataRowQuery​(Class<?> entityType, Expression expression)` | Creates a ObjectSelect that fetches data for an `ObjEntity` determined from a provided class and uses provided expression for its qualifier.
    `ObjectSelect<T>` | `dbEntityName​(String dbEntityName)` | Sets the `DbEntity` name to fetch without changing the return type of the query.
    `static ObjectSelect<DataRow>` | `dbQuery​(String dbEntityName)` | Creates a ObjectSelect that fetches DataRows for a `DbEntity` determined from provided "dbEntityName".
    `static ObjectSelect<DataRow>` | `dbQuery​(String dbEntityName, Expression expression)` | Creates a ObjectSelect that fetches DataRows for a `DbEntity` determined from provided "dbEntityName" and uses provided expression for its qualifier.
    `ObjectSelect<T>` | `entityName​(String entityName)` | Sets the `ObjEntity` name to fetch without changing the return type of the query.
    `ObjectSelect<T>` | `entityType​(Class<?> entityType)` | Sets the type of the entity to fetch without changing the return type of the query.
    `ObjectSelect<DataRow>` | `fetchDataRows()` | Forces query to fetch DataRows.
    `boolean` | `isFetchingDataRows()` |
    `ObjectSelect<T>` | `limit​(int fetchLimit)` | Resets query fetch limit - a parameter that defines max number of objects that should be ever be fetched from the database.
    `ObjectSelect<T>` | `localCache()` | Instructs Cayenne to look for query results in the "local" cache when running the query.
    `ObjectSelect<T>` | `localCache​(String cacheGroup)` | Instructs Cayenne to look for query results in the "local" cache when running the query.
    `<E> ColumnSelect<E>` | `max​(Property<E> property)` | Select maximum value of property
    `<E> ColumnSelect<E>` | `min​(Property<E> property)` | Select minimum value of property
    `ObjectSelect<T>` | `offset​(int fetchOffset)` | Resets query fetch offset - a parameter that defines how many objects should be skipped when reading data from the database.
    `ObjectSelect<T>` | `or​(Collection<Expression> expressions)` | OR's provided expressions to the existing WHERE clause expression.
    `ObjectSelect<T>` | `or​(Expression... expressions)` | OR's provided expressions to the existing WHERE clause expression.
    `ObjectSelect<T>` | `orderBy​(String property)` | Add an ascending ordering on the given property.
    `ObjectSelect<T>` | `orderBy​(String property, SortOrder sortOrder)` | Add an ordering on the given property.
    `ObjectSelect<T>` | `orderBy​(Collection<Ordering> orderings)` | Adds a list of orderings to this query.
    `ObjectSelect<T>` | `orderBy​(Ordering... orderings)` | Add one or more orderings to this query.
    `ObjectSelect<T>` | `pageSize​(int pageSize)` | Resets query page size.
    `ObjectSelect<T>` | `prefetch​(String path, int semantics)` | Merges a prefetch path with specified semantics into the query prefetch tree.
    `ObjectSelect<T>` | `prefetch​(PrefetchTreeNode prefetch)` | Merges prefetch into the query prefetch tree.
    `static <T> ObjectSelect<T>` | `query​(Class<T> entityType)` | Creates a ObjectSelect that selects objects of a given persistent class.
    `static <T> ObjectSelect<T>` | `query​(Class<T> resultType, String entityName)` | Creates a ObjectSelect that fetches data for `ObjEntity` determined from provided "entityName", but fetches the result of a provided type.
    `static <T> ObjectSelect<T>` | `query​(Class<T> entityType, Expression expression)` | Creates a ObjectSelect that selects objects of a given persistent class and uses provided expression for its qualifier.
    `static <T> ObjectSelect<T>` | `query​(Class<T> entityType, Expression expression, List<Ordering> orderings)` | Creates a ObjectSelect that selects objects of a given persistent class and uses provided expression for its qualifier.
    `long` | `selectCount​(ObjectContext context)` | Quick way to select count of records
    `T` | `selectFirst​(ObjectContext context)` | Selects a single object using provided context.
    `ObjectSelect<T>` | `sharedCache()` | Instructs Cayenne to look for query results in the "shared" cache when running the query.
    `ObjectSelect<T>` | `sharedCache​(String cacheGroup)` | Instructs Cayenne to look for query results in the "shared" cache when running the query.
    `ObjectSelect<T>` | `statementFetchSize​(int size)` | Sets fetch size of the PreparedStatement generated for this query.
    `<E extends Number> ColumnSelect<E>` | `sum​(Property<E> property)` | Select sum of values
    `ObjectSelect<T>` | `where​(String expressionString, Object... parameters)` | Appends a qualifier expression of this query, using provided expression String and an array of position parameters.
    `ObjectSelect<T>` | `where​(Expression expression)` | Appends a qualifier expression of this query.

    - ### Methods inherited from class org.apache.cayenne.query.FluentSelect

      `batchIterator, getCacheGroup, getCacheStrategy, getDbEntityName, getEntityName, getEntityType, getLimit, getOffset, getOrderings, getPageSize, getPrefetches, getStatementFetchSize, getWhere, iterate, iterator, select, selectOne`

    - ### Methods inherited from class org.apache.cayenne.query.IndirectQuery

      `createSQLAction, getMetaData, getReplacementQuery, route`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.apache.cayenne.query.Query

      `createSQLAction, getMetaData, route`

- - ### Field Detail

    - #### fetchingDataRows

          protected boolean fetchingDataRows

  - ### Constructor Detail

    - #### ObjectSelect

          protected ObjectSelect()

  - ### Method Detail

    - #### query

          public static <T> ObjectSelect<T> query​(Class<T> entityType)

      Creates a ObjectSelect that selects objects of a given persistent class.

    - #### query

          public static <T> ObjectSelect<T> query​(Class<T> entityType,
                                                  Expression expression)

      Creates a ObjectSelect that selects objects of a given persistent class and uses provided expression for its qualifier.

    - #### query

          public static <T> ObjectSelect<T> query​(Class<T> entityType,
                                                  Expression expression,
                                                  List<Ordering> orderings)

      Creates a ObjectSelect that selects objects of a given persistent class and uses provided expression for its qualifier.

    - #### dataRowQuery

          public static ObjectSelect<DataRow> dataRowQuery​(Class<?> entityType)

      Creates a ObjectSelect that fetches data for an `ObjEntity` determined from a provided class.

    - #### dataRowQuery

          public static ObjectSelect<DataRow> dataRowQuery​(Class<?> entityType,
                                                           Expression expression)

      Creates a ObjectSelect that fetches data for an `ObjEntity` determined from a provided class and uses provided expression for its qualifier.

    - #### query

          public static <T> ObjectSelect<T> query​(Class<T> resultType,
                                                  String entityName)

      Creates a ObjectSelect that fetches data for `ObjEntity` determined from provided "entityName", but fetches the result of a provided type. This factory method is most often used with generic classes that by themselves are not enough to resolve the entity to fetch.

    - #### dbQuery

          public static ObjectSelect<DataRow> dbQuery​(String dbEntityName)

      Creates a ObjectSelect that fetches DataRows for a `DbEntity` determined from provided "dbEntityName".

    - #### dbQuery

          public static ObjectSelect<DataRow> dbQuery​(String dbEntityName,
                                                      Expression expression)

      Creates a ObjectSelect that fetches DataRows for a `DbEntity` determined from provided "dbEntityName" and uses provided expression for its qualifier.
      Returns:
        this object

    - #### columnQuery

          public static <E> ColumnSelect<E> columnQuery​(Class<?> entityType,
                                                        Property<E> column)

      Creates a ColumnSelect that will fetch single property that can be resolved against a given `ObjEntity` class.
      Parameters:
        `entityType` - base persistent class that will be used as a root for this query
        `column` - single column to select

    - #### columnQuery

          public static ColumnSelect<Object[]> columnQuery​(Class<?> entityType,
                                                           Property<?> firstColumn,
                                                           Property<?>... otherColumns)

      Creates a ColumnSelect that will fetch multiple columns of a given `ObjEntity`
      Parameters:
        `entityType` - base persistent class that will be used as a root for this query
        `firstColumn` - column to select
        `otherColumns` - columns to select

    - #### createReplacementQuery

          protected Query createReplacementQuery​(EntityResolver resolver)

      Translates self to a SelectQuery.
      Overrides:
        `createReplacementQuery` in class `FluentSelect<T>`

    - #### entityType

          public ObjectSelect<T> entityType​(Class<?> entityType)

      Sets the type of the entity to fetch without changing the return type of the query.
      Returns:
        this object

    - #### entityName

          public ObjectSelect<T> entityName​(String entityName)

      Sets the `ObjEntity` name to fetch without changing the return type of the query. This form is most often used for generic entities that don't map to a distinct class.
      Returns:
        this object

    - #### dbEntityName

          public ObjectSelect<T> dbEntityName​(String dbEntityName)

      Sets the `DbEntity` name to fetch without changing the return type of the query. This form is most often used for generic entities that don't map to a distinct class.
      Returns:
        this object

    - #### where

          public ObjectSelect<T> where​(Expression expression)

      Appends a qualifier expression of this query. An equivalent to `and(Expression...)` that can be used a syntactic sugar.
      Returns:
        this object

    - #### where

          public ObjectSelect<T> where​(String expressionString,
                                       Object... parameters)

      Appends a qualifier expression of this query, using provided expression String and an array of position parameters. This is an equivalent to calling "and".
      Returns:
        this object

    - #### and

          public ObjectSelect<T> and​(Expression... expressions)

      AND's provided expressions to the existing WHERE clause expression.
      Returns:
        this object

    - #### and

          public ObjectSelect<T> and​(Collection<Expression> expressions)

      AND's provided expressions to the existing WHERE clause expression.
      Returns:
        this object

    - #### or

          public ObjectSelect<T> or​(Expression... expressions)

      OR's provided expressions to the existing WHERE clause expression.
      Returns:
        this object

    - #### or

          public ObjectSelect<T> or​(Collection<Expression> expressions)

      OR's provided expressions to the existing WHERE clause expression.
      Returns:
        this object

    - #### orderBy

          public ObjectSelect<T> orderBy​(String property)

      Add an ascending ordering on the given property. If there is already an ordering on this query then add this ordering with a lower priority.
      Parameters:
        `property` - the property to sort on
      Returns:
        this object

    - #### orderBy

          public ObjectSelect<T> orderBy​(String property,
                                         SortOrder sortOrder)

      Add an ordering on the given property. If there is already an ordering on this query then add this ordering with a lower priority.
      Parameters:
        `property` - the property to sort on
        `sortOrder` - the direction of the ordering
      Returns:
        this object

    - #### orderBy

          public ObjectSelect<T> orderBy​(Ordering... orderings)

      Add one or more orderings to this query.
      Returns:
        this object

    - #### orderBy

          public ObjectSelect<T> orderBy​(Collection<Ordering> orderings)

      Adds a list of orderings to this query.
      Returns:
        this object

    - #### prefetch

          public ObjectSelect<T> prefetch​(PrefetchTreeNode prefetch)

      Merges prefetch into the query prefetch tree.
      Returns:
        this object

    - #### prefetch

          public ObjectSelect<T> prefetch​(String path,
                                          int semantics)

      Merges a prefetch path with specified semantics into the query prefetch tree.
      Returns:
        this object

    - #### limit

          public ObjectSelect<T> limit​(int fetchLimit)

      Resets query fetch limit - a parameter that defines max number of objects that should be ever be fetched from the database.

    - #### offset

          public ObjectSelect<T> offset​(int fetchOffset)

      Resets query fetch offset - a parameter that defines how many objects should be skipped when reading data from the database.

    - #### pageSize

          public ObjectSelect<T> pageSize​(int pageSize)

      Resets query page size. A non-negative page size enables query result pagination that saves memory and processing time for large lists if only parts of the result are ever going to be accessed.

    - #### statementFetchSize

          public ObjectSelect<T> statementFetchSize​(int size)

      Sets fetch size of the PreparedStatement generated for this query. Only non-negative values would change the default size.
      See Also:
        `Statement.setFetchSize(int)`

    - #### cacheStrategy

          public ObjectSelect<T> cacheStrategy​(QueryCacheStrategy strategy)

    - #### cacheStrategy

          public ObjectSelect<T> cacheStrategy​(QueryCacheStrategy strategy,
                                               String cacheGroup)

    - #### cacheGroup

          public ObjectSelect<T> cacheGroup​(String cacheGroup)

    - #### localCache

          public ObjectSelect<T> localCache​(String cacheGroup)

      Instructs Cayenne to look for query results in the "local" cache when running the query. This is a short-hand notation for:

           query.cacheStrategy(QueryCacheStrategy.LOCAL_CACHE, cacheGroup);

    - #### localCache

          public ObjectSelect<T> localCache()

      Instructs Cayenne to look for query results in the "local" cache when running the query. This is a short-hand notation for:

           query.cacheStrategy(QueryCacheStrategy.LOCAL_CACHE);

    - #### sharedCache

          public ObjectSelect<T> sharedCache​(String cacheGroup)

      Instructs Cayenne to look for query results in the "shared" cache when running the query. This is a short-hand notation for:

           query.cacheStrategy(QueryCacheStrategy.SHARED_CACHE, cacheGroup);

    - #### sharedCache

          public ObjectSelect<T> sharedCache()

      Instructs Cayenne to look for query results in the "shared" cache when running the query. This is a short-hand notation for:

           query.cacheStrategy(QueryCacheStrategy.SHARED_CACHE);

    - #### fetchDataRows

          public ObjectSelect<DataRow> fetchDataRows()

      Forces query to fetch DataRows. This automatically changes whatever result type was set previously to "DataRow".
      Returns:
        this object

    - #### columns

          public ColumnSelect<Object[]> columns​(Property<?> firstProperty,
                                                Property<?>... properties)

      Select only specific properties.

      Can be any properties that can be resolved against root entity type (root entity's properties, function call expressions, properties of relationships, etc).

           List<Object[]> columns = ObjectSelect.query(Artist.class)
                                              .columns(Artist.ARTIST_NAME, Artist.DATE_OF_BIRTH)
                                              .select(context);

      Parameters:
        `properties` - array of properties to select
      See Also:
        `column(Property)`

    - #### column

          public <E> ColumnSelect<E> column​(Property<E> property)

      Select one specific property.

      Can be any property that can be resolved against root entity type (root entity's property, function call expression, property of relationships, etc)

      If you need several columns use `columns(Property, Property[])` method.

           List<String> names = ObjectSelect.query(Artist.class)
                                            .column(Artist.ARTIST_NAME)
                                            .select(context);

      Parameters:
        `property` - single property to select
      See Also:
        `columns(Property, Property[])`

    - #### count

          public ColumnSelect<Long> count()

      Select COUNT(*)
      See Also:
        `column(Property)`

    - #### count

          public ColumnSelect<Long> count​(Property<?> property)

      Select COUNT(property)

      Can return different result than COUNT(*) as it will count only non null values

      See Also:
        `count()`, `column(Property)`

    - #### min

          public <E> ColumnSelect<E> min​(Property<E> property)

      Select minimum value of property

      See Also:
        `column(Property)`

    - #### max

          public <E> ColumnSelect<E> max​(Property<E> property)

      Select maximum value of property

      See Also:
        `column(Property)`

    - #### avg

          public <E> ColumnSelect<E> avg​(Property<E> property)

      Select average value of property

      See Also:
        `column(Property)`

    - #### sum

          public <E extends Number> ColumnSelect<E> sum​(Property<E> property)

      Select sum of values

      See Also:
        `column(Property)`

    - #### selectCount

          public long selectCount​(ObjectContext context)

      Quick way to select count of records

      Usage:

               long count = ObjectSelect.query(Artist.class)
                                .where(Artist.ARTIST_NAME.like("a%"))
                                .selectCount(context);

      Parameters:
        `context` - to perform query
      Returns:
        count of rows

    - #### selectFirst

          public T selectFirst​(ObjectContext context)

      Description copied from interface: `Select`
      Selects a single object using provided context. The query itself can match any number of objects, but will return only the first one. It returns null if no objects were matched.

      If it matched more than one object, the first object from the list is returned. This makes 'selectFirst' different from `Select.selectOne(ObjectContext)`, which would throw in this situation. 'selectFirst' is useful e.g. when the query is ordered and we only want to see the first object (e.g. "most recent news article"), etc.

      Selecting the first object via "Select.selectFirst(ObjectContext)" is more comprehensible than selecting via "ObjectContext.selectFirst(Select)", because implementations of "Select" set fetch size limit to one.

    - #### isFetchingDataRows

          public boolean isFetchingDataRows()