# Class ObjectSelect<T>

java.lang.Object
org.apache.cayenne.query.CacheableQuery
org.apache.cayenne.query.AbstractQuery
org.apache.cayenne.query.FluentSelect<T, ObjectSelect<T>>
org.apache.cayenne.query.ObjectSelect<T>

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

Direct Known Subclasses:
  `PrefetchSelectQuery`

public class ObjectSelect<T> extends FluentSelect<T, ObjectSelect<T>> implements ParameterizedQuery
A selecting query providing chainable 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 inherited from class FluentSelect

  `dbEntityName, distinct, entityName, entityType, having, havingExpressionIsActive, metaData, orderings, where`

  ### Fields inherited from class AbstractQuery

  `root`

  ### Fields inherited from class CacheableQuery

  `LOGGER`

- ## Constructor Summary

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

- ## Method Summary

  Modifier and Type
  Method
  Description
  `<E> ColumnSelect<E>`
  `aggregate(BaseProperty<E> property, String function, Class<E> type)`
  Select result of some function, that aggregates values.
  `<E extends Number>
  ColumnSelect<E>`
  `avg(NumericProperty<E> property)`
  Select average value of property
  `<E> ColumnSelect<E>`
  `column(Property<E> property)`
  Select one specific property.
  `static ColumnSelect<Object[]>`
  `columnQuery(Class<?> entityType, Property<?>... columns)`
  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<?>... properties)`
  Select only specific properties.
  `ColumnSelect<Long>`
  `count()`
  Select COUNT(*)
  `ColumnSelect<Long>`
  `count(BaseProperty<?> property)`
  Select COUNT(property)
  `protected org.apache.cayenne.query.ObjectSelectMetadata`
  `createMetadata()`
  `Query`
  `createQuery(Map<String,?> parameters)`
  This method is intended for internal use in a `MappedSelect`.
  `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.
  `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>`
  `distinct()`
  `ObjectSelect<DataRow>`
  `fetchDataRows()`
  Forces query to fetch DataRows.
  `protected org.apache.cayenne.query.ObjectSelectMetadata`
  `getBaseMetaData()`
  `QueryMetadata`
  `getMetaData(EntityResolver resolver)`
  Returns default select parameters.
  `ObjectSelect<T>`
  `having(String expressionString, Object... parameters)`
  Appends a having qualifier expression of this query, using provided expression String and an array of position parameters.
  `ObjectSelect<T>`
  `having(Expression expression)`
  Appends a having qualifier expression of this query.
  `<E> ColumnSelect<E>`
  `max(ComparableProperty<E> property)`
  Select maximum value of property
  `<E extends Number>
  ColumnSelect<E>`
  `max(NumericProperty<E> property)`
  Select maximum value of property
  `<E> ColumnSelect<E>`
  `min(ComparableProperty<E> property)`
  Select minimum value of property
  `<E extends Number>
  ColumnSelect<E>`
  `min(NumericProperty<E> property)`
  Select minimum value of property
  `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.
  `<E extends Number>
  ColumnSelect<E>`
  `sum(NumericProperty<E> property)`
  Select sum of values

  ### Methods inherited from class FluentSelect

  `and, and, batchIterator, cacheGroup, cacheStrategy, cacheStrategy, castSelf, createSQLAction, dbEntityName, entityName, entityType, getActiveExpression, getColumns, getDbEntityName, getEntityName, getEntityType, getHaving, getLimit, getOffset, getOrderings, getPageSize, getPrefetches, getQueryTimeout, getStatementFetchSize, getWhere, initWithProperties, isDistinct, isFetchingDataRows, iterate, iterator, joinExpression, limit, localCache, localCache, offset, or, or, orderBy, orderBy, orderBy, orderBy, pageSize, prefetch, prefetch, queryTimeout, resolveRoot, route, routePrefetches, select, selectOne, setActiveExpression, sharedCache, sharedCache, statementFetchSize, where, where`

  ### Methods inherited from class AbstractQuery

  `getRoot, setRoot, toString`

  ### Methods inherited from class CacheableQuery

  `getCacheGroup, getCacheStrategy, setCacheGroup, setCacheStrategy, useLocalCache, useLocalCache, useSharedCache, useSharedCache`

  ### Methods inherited from class Object

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

  ### Methods inherited from interface Query

  `createSQLAction, route`

- ## Constructor Details

  - ### ObjectSelect

    protected ObjectSelect()

- ## Method Details

  - ### 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<?>... columns)
    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
      `columns` - columns to select

  - ### createMetadata

    protected org.apache.cayenne.query.ObjectSelectMetadata createMetadata()
    Specified by:
      `createMetadata` in class `FluentSelect<T, ObjectSelect<T>>`

  - ### having

    public ObjectSelect<T> having(Expression expression)
    Appends a having qualifier expression of this query. An equivalent to `FluentSelect.and(Expression...)` that can be used a syntactic sugar.
    Returns:
      this object
    Since:
      4.2

  - ### having

    public ObjectSelect<T> having(String expressionString, Object... parameters)
    Appends a having 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
    Since:
      4.2

  - ### 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<?>... 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[])` method.

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

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

  - ### count

    public ColumnSelect<Long> count()
    Select COUNT(*)
    See Also:
      - `column(Property)`

  - ### count

    public ColumnSelect<Long> count(BaseProperty<?> 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(ComparableProperty<E> property)

    Select minimum value of property

    See Also:
      - `column(Property)`

  - ### min

    public <E extends Number> ColumnSelect<E> min(NumericProperty<E> property)

    Select minimum value of property

    See Also:
      - `column(Property)`

  - ### max

    public <E> ColumnSelect<E> max(ComparableProperty<E> property)

    Select maximum value of property

    See Also:
      - `column(Property)`

  - ### max

    public <E extends Number> ColumnSelect<E> max(NumericProperty<E> property)

    Select maximum value of property

    See Also:
      - `column(Property)`

  - ### avg

    public <E extends Number> ColumnSelect<E> avg(NumericProperty<E> property)

    Select average value of property

    See Also:
      - `column(Property)`

  - ### sum

    public <E extends Number> ColumnSelect<E> sum(NumericProperty<E> property)

    Select sum of values

    See Also:
      - `column(Property)`

  - ### aggregate

    public <E> ColumnSelect<E> aggregate(BaseProperty<E> property, String function, Class<E> type)

    Select result of some function, that aggregates values.

    Since:
      5.0
    See Also:
      - `column(Property)`

  - ### distinct

    public ObjectSelect<T> distinct()
    Returns:
      this
    Since:
      4.2

  - ### 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.

    Specified by:
      `selectFirst` in interface `Select<T>`

  - ### getMetaData

    public QueryMetadata getMetaData(EntityResolver resolver)
    Description copied from class: `AbstractQuery`
    Returns default select parameters.
    Specified by:
      `getMetaData` in interface `Query`
    Overrides:
      `getMetaData` in class `AbstractQuery`

  - ### getBaseMetaData

    protected org.apache.cayenne.query.ObjectSelectMetadata getBaseMetaData()
    Specified by:
      `getBaseMetaData` in class `CacheableQuery`

  - ### createQuery

    public Query createQuery(Map<String,?> parameters)
    This method is intended for internal use in a `MappedSelect`.
    Specified by:
      `createQuery` in interface `ParameterizedQuery`
    Parameters:
      `parameters` - to apply
    Returns:
      this query with parameters applied to the where qualifier
    Since:
      4.2