Class ColumnSelect<T>

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

public class ColumnSelect<T> extends FluentSelect<T,ColumnSelect<T>>

A helper builder for queries selecting individual properties based on the root object.

It can be used to select properties of the object itself, properties of related entities or some function calls (including aggregate functions).

Usage examples:

 
      // select list of names:
      List<String> names = ObjectSelect.columnQuery(Artist.class, Artist.ARTIST_NAME).select(context);

      // select count:
      long count = ObjectSelect.columnQuery(Artist.class, PropertyFactory.COUNT).selectOne();

      // select only required properties of an entity:
      List<Object[]> data = ObjectSelect.columnQuery(Artist.class, Artist.ARTIST_NAME, Artist.DATE_OF_BIRTH)
                                  .where(Artist.ARTIST_NAME.like("Picasso%))
                                  .select(context);
 
 

Note: this class can't be instantiated directly. Use ObjectSelect.

Since:
4.0
See Also:
  • Field Details

    • columns

      protected Collection<Property<?>> columns
    • singleColumn

      protected boolean singleColumn
  • Constructor Details

    • ColumnSelect

      protected ColumnSelect()
    • ColumnSelect

      protected ColumnSelect(ObjectSelect<T> select)
      Copy constructor to convert ObjectSelect to ColumnSelect
  • Method Details

    • createMetadata

      protected org.apache.cayenne.query.ColumnSelectMetadata createMetadata()
      Specified by:
      createMetadata in class FluentSelect<T,ColumnSelect<T>>
    • columns

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

      Add properties to select.

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

       
       List<Object[]> columns = ObjectSelect.columnQuery(Artist.class, Artist.ARTIST_NAME)
                                          .columns(Artist.ARTIST_SALARY, Artist.DATE_OF_BIRTH)
                                          .select(context);
       
       
      Parameters:
      properties - array of properties to select
      See Also:
    • columns

      public ColumnSelect<Object[]> columns(Collection<Property<?>> properties)

      Add properties to select.

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

      Parameters:
      properties - collection of properties, must contain at least one element
      See Also:
    • column

      protected <E> ColumnSelect<E> column(Property<E> property)
    • count

      public ColumnSelect<Object[]> count()

      Shortcut for columns(Property[]) columns}(Property.COUNT)

    • count

      public ColumnSelect<Object[]> count(BaseProperty<?> property)

      Select COUNT(property)

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

      See Also:
    • min

      public ColumnSelect<Object[]> min(ComparableProperty<?> property)

      Select minimum value of property

      See Also:
    • max

      public ColumnSelect<Object[]> max(ComparableProperty<?> property)

      Select maximum value of property

      See Also:
    • avg

      public ColumnSelect<Object[]> avg(NumericProperty<?> property)

      Select average value of property

      See Also:
    • sum

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

      Select sum of values

      See Also:
    • aggregate

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

      Select result of some function, that aggregates values.

      Since:
      5.0
      See Also:
    • having

      public ColumnSelect<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
    • having

      public ColumnSelect<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
    • distinct

      public ColumnSelect<T> distinct()
      Explicitly request distinct in query.
    • suppressDistinct

      public ColumnSelect<T> suppressDistinct()
      Explicitly suppress distinct in query.
    • getColumns

      public Collection<Property<?>> getColumns()
      Overrides:
      getColumns in class FluentSelect<T,ColumnSelect<T>>
    • 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.

    • 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.ColumnSelectMetadata getBaseMetaData()
      Specified by:
      getBaseMetaData in class CacheableQuery
    • map

      public <E> ColumnSelect<E> map(Function<T,E> mapper)
      Maps result of this query by processing with a given function.
      Could be used to map plain Object[] to some domain-specific object.
      Note: this method could be called multiple time, result will be mapped by all functions in the call order.
      Type Parameters:
      E - new result type
      Parameters:
      mapper - function that maps result to the required type.
      Returns:
      this query with changed result type
      Since:
      4.2