Class ObjectSelect<T>
- Direct Known Subclasses:
PrefetchSelectQuery
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
-
Field Summary
Fields inherited from class FluentSelect
dbEntityName, distinct, entityName, entityType, having, havingExpressionIsActive, metaData, orderings, where -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<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> Select one specific property.static ColumnSelect<Object[]> columnQuery(Class<?> entityType, Property<?>... columns) Creates a ColumnSelect that will fetch multiple columns of a givenObjEntitystatic <E> ColumnSelect<E> columnQuery(Class<?> entityType, Property<E> column) Creates a ColumnSelect that will fetch single property that can be resolved against a givenObjEntityclass.Select only specific properties.count()Select COUNT(*)count(BaseProperty<?> property) Select COUNT(property)protected org.apache.cayenne.query.ObjectSelectMetadatastatic ObjectSelect<DataRow> dataRowQuery(Class<?> entityType) Creates a ObjectSelect that fetches data for anObjEntitydetermined from a provided class.static ObjectSelect<DataRow> dataRowQuery(Class<?> entityType, Expression expression) Deprecated, for removal: This API element is subject to removal in a future version.static ObjectSelect<DataRow> Creates a ObjectSelect that fetches DataRows for aDbEntitydetermined from provided "dbEntityName".static ObjectSelect<DataRow> dbQuery(String dbEntityName, Expression expression) Deprecated, for removal: This API element is subject to removal in a future version.usedbQuery(String)and thenFluentSelect.wheredistinct()Forces query to fetch DataRows.protected org.apache.cayenne.query.ObjectSelectMetadatagetMetaData(EntityResolver resolver) Returns query runtime parameters.Appends a having qualifier expression of this query, using provided expression String and an array of position parameters.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 propertystatic <T> ObjectSelect<T> Parses a String of a query that fetches root objects, i.e. a query with no "select" clause, or with "select self".static <T> ObjectSelect<T> Parses a String of a query that fetches root objects, binding its parameters by name.static FluentSelect<?, ?> Parses a query String, creating either an ObjectSelect or a ColumnSelect, depending on the "select" clause of the String.static FluentSelect<?, ?> Parses a query String, binding the values of its "$name" parameters by name.static <T> ColumnSelect<T> parseColumn(Class<T> type, String query, Object... params) Parses a String of a query that fetches a single column.static <T> ColumnSelect<T> parseColumn(Class<T> type, String query, Map<String, ?> params) Parses a String of a query that fetches a single column, binding its parameters by name.static ColumnSelect<Object[]> parseColumns(String query, Object... params) Parses a String of a query that fetches multiple columns.static <T> ObjectSelect<T> Creates a ObjectSelect that selects objects of a given persistent class.static <T> ObjectSelect<T> Creates a ObjectSelect that fetches data forObjEntitydetermined from provided "entityName", but fetches the result of a provided type.static <T> ObjectSelect<T> query(Class<T> entityType, Expression expression) Deprecated, for removal: This API element is subject to removal in a future version.usequery(Class)and thenFluentSelect.wherestatic <T> ObjectSelect<T> query(Class<T> entityType, Expression expression, List<Ordering> orderings) Deprecated, for removal: This API element is subject to removal in a future version.longselectCount(ObjectContext context) Quick way to select count of recordsselectFirst(ObjectContext context) Selects a single object using provided context.<E extends Number>
ColumnSelect<E> sum(NumericProperty<E> property) Select sum of valuesMethods inherited from class FluentSelect
and, and, byId, byIds, byIds, cacheGroup, cacheStrategy, cacheStrategy, castSelf, createSQLAction, dbEntityName, entityName, entityType, getActiveExpression, getCacheGroup, getCacheStrategy, getColumns, getDbEntityName, getEntityName, getEntityType, getHaving, getLimit, getOffset, getOrderings, getPageSize, getPrefetches, getQueryTimeout, getStatementFetchSize, getWhere, isDistinct, isFetchingDataRows, joinExpression, limit, localCache, localCache, offset, or, or, orderBy, orderBy, orderBy, orderBy, pageSize, prefetch, prefetch, queryTimeout, resolveRoot, route, routePrefetches, setActiveExpression, setCacheGroup, setCacheStrategy, sharedCache, sharedCache, statementFetchSize, useLocalCache, useLocalCache, useSharedCache, useSharedCache, where, where
-
Constructor Details
-
ObjectSelect
protected ObjectSelect()
-
-
Method Details
-
query
Creates a ObjectSelect that selects objects of a given persistent class. -
query
@Deprecated(since="5.0", forRemoval=true) public static <T> ObjectSelect<T> query(Class<T> entityType, Expression expression) Deprecated, for removal: This API element is subject to removal in a future version.usequery(Class)and thenFluentSelect.whereCreates a ObjectSelect that selects objects of a given persistent class and uses provided expression for its qualifier. -
query
@Deprecated(since="5.0", forRemoval=true) public static <T> ObjectSelect<T> query(Class<T> entityType, Expression expression, List<Ordering> orderings) Deprecated, for removal: This API element is subject to removal in a future version.Creates a ObjectSelect that selects objects of a given persistent class and uses provided expression for its qualifier. -
dataRowQuery
Creates a ObjectSelect that fetches data for anObjEntitydetermined from a provided class. -
dataRowQuery
@Deprecated(since="5.0", forRemoval=true) public static ObjectSelect<DataRow> dataRowQuery(Class<?> entityType, Expression expression) Deprecated, for removal: This API element is subject to removal in a future version.usedataRowQuery(Class)and thenFluentSelect.whereCreates a ObjectSelect that fetches data for anObjEntitydetermined from a provided class and uses provided expression for its qualifier. -
query
Creates a ObjectSelect that fetches data forObjEntitydetermined 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
Creates a ObjectSelect that fetches DataRows for aDbEntitydetermined from provided "dbEntityName". -
dbQuery
@Deprecated(since="5.0", forRemoval=true) public static ObjectSelect<DataRow> dbQuery(String dbEntityName, Expression expression) Deprecated, for removal: This API element is subject to removal in a future version.usedbQuery(String)and thenFluentSelect.whereCreates a ObjectSelect that fetches DataRows for aDbEntitydetermined from provided "dbEntityName" and uses provided expression for its qualifier. -
columnQuery
Creates a ColumnSelect that will fetch single property that can be resolved against a givenObjEntityclass.- Parameters:
entityType- base persistent class that will be used as a root for this querycolumn- single column to select
-
columnQuery
Creates a ColumnSelect that will fetch multiple columns of a givenObjEntity- Parameters:
entityType- base persistent class that will be used as a root for this querycolumns- columns to select
-
parse
Parses a query String, creating either an ObjectSelect or a ColumnSelect, depending on the "select" clause of the String. The general form of the query is[select [distinct] columns] from Entity [where exp] [having exp] [order by orderings] [limit n] [offset m] [prefetch paths]
where all the expressions follow the syntax ofExpressionFactory.exp(String, Object...). A query with no "select" clause, or with "select self" results in an ObjectSelect. E.g.:ObjectSelect.parse("from Artist where artistName like $name order by dateOfBirth desc limit 10", "A%"); ObjectSelect.parse("select artistName, count(paintingArray) from Artist");- Parameters:
query- a query Stringparams- values of the "$name" parameters in the String, bound in the order of their first occurrence. With no values, the parameters are preserved in the query, to be bound later, e.g. withExpression.params(Map)on its "where" clause- Since:
- 5.0
- See Also:
-
parse
Parses a query String, binding the values of its "$name" parameters by name. A parameter that has no value in the map is preserved, and the smallest condition of a "where" or "having" clause that contains it is dropped from the query, in the subqueries alike. So the same String can be used with and without its optional parameters. A null value in the map is bound as is.- Parameters:
query- a query Stringparams- values of the "$name" parameters in the String by name, or null to preserve the parameters in the query, likeparse(String, Object...)with no values- Since:
- 5.0
- See Also:
-
parse
Parses a String of a query that fetches root objects, i.e. a query with no "select" clause, or with "select self". If the type is DataRow, the query will fetch DataRows.- Parameters:
type- result type of the query- Since:
- 5.0
- See Also:
-
parse
Parses a String of a query that fetches root objects, binding its parameters by name.- Parameters:
type- result type of the query- Since:
- 5.0
- See Also:
-
parseColumn
Parses a String of a query that fetches a single column.- Parameters:
type- result type of the query- Since:
- 5.0
- See Also:
-
parseColumn
Parses a String of a query that fetches a single column, binding its parameters by name.- Parameters:
type- result type of the query- Since:
- 5.0
- See Also:
-
parseColumns
Parses a String of a query that fetches multiple columns.- Since:
- 5.0
- See Also:
-
createMetadata
protected org.apache.cayenne.query.ObjectSelectMetadata createMetadata()- Specified by:
createMetadatain classFluentSelect<T, ObjectSelect<T>>
-
having
Appends a having qualifier expression of this query. An equivalent toFluentSelect.and(Expression...)that can be used a syntactic sugar.- Returns:
- this object
- Since:
- 4.2
-
having
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
Forces query to fetch DataRows. This automatically changes whatever result type was set previously to "DataRow".- Returns:
- this object
-
columns
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
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:
-
count
-
count
Select COUNT(property)
Can return different result than COUNT(*) as it will count only non null values
- See Also:
-
min
Select minimum value of property
- See Also:
-
min
Select minimum value of property
- See Also:
-
max
Select maximum value of property
- See Also:
-
max
Select maximum value of property
- See Also:
-
avg
Select average value of property
- See Also:
-
sum
Select sum of values
- See Also:
-
aggregate
Select result of some function, that aggregates values.
- Since:
- 5.0
- See Also:
-
distinct
- Returns:
- this
- Since:
- 4.2
-
selectCount
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
Description copied from interface:SelectSelects 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
Description copied from interface:QueryReturns query runtime parameters. The method is called at various stages of the execution by Cayenne access stack to retrieve query parameters. EntityResolver instance is passed to this method, meaning that the query doesn't need to store direct references to Cayenne mapping objects and can resolve them at runtime. -
getBaseMetaData
protected org.apache.cayenne.query.ObjectSelectMetadata getBaseMetaData()- Specified by:
getBaseMetaDatain classFluentSelect<T, ObjectSelect<T>>
-
dataRowQuery(Class)and thenFluentSelect.where