Class ObjectSelect<T>
- All Implemented Interfaces:
Serializable
,ParameterizedQuery
,Query
,Select<T>
- Direct Known Subclasses:
PrefetchSelectQuery
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:
-
Field Summary
Modifier and TypeFieldDescriptionprotected org.apache.cayenne.query.ObjectSelectMetadata
Fields inherited from class org.apache.cayenne.query.FluentSelect
dbEntityName, entityName, entityType, having, orderings, where
Fields inherited from class org.apache.cayenne.query.AbstractQuery
root
Fields inherited from class org.apache.cayenne.query.CacheableQuery
logger
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionand
(Collection<Expression> expressions) AND's provided expressions to the existing WHERE clause expression.and
(Expression... expressions) AND's provided expressions to the existing WHERE clause expression.<E extends Number>
ColumnSelect<E> avg
(NumericProperty<E> property) Select average value of property<E> ColumnSelect
<E> Deprecated.cacheGroup
(String cacheGroup) cacheStrategy
(QueryCacheStrategy strategy) cacheStrategy
(QueryCacheStrategy strategy, String cacheGroup) <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 givenObjEntity
static <E> ColumnSelect
<E> columnQuery
(Class<?> entityType, Property<E> column) Creates a ColumnSelect that will fetch single property that can be resolved against a givenObjEntity
class.Select only specific properties.count()
Select COUNT(*)count
(BaseProperty<?> property) Select COUNT(property)createQuery
(Map<String, ?> parameters) This method is intended for internal use in aMappedSelect
.static ObjectSelect
<DataRow> dataRowQuery
(Class<?> entityType) Creates a ObjectSelect that fetches data for anObjEntity
determined from a provided class.static ObjectSelect
<DataRow> dataRowQuery
(Class<?> entityType, Expression expression) Creates a ObjectSelect that fetches data for anObjEntity
determined from a provided class and uses provided expression for its qualifier.dbEntityName
(String dbEntityName) Sets theDbEntity
name to fetch without changing the return type of the query.static ObjectSelect
<DataRow> Creates a ObjectSelect that fetches DataRows for aDbEntity
determined from provided "dbEntityName".static ObjectSelect
<DataRow> dbQuery
(String dbEntityName, Expression expression) Creates a ObjectSelect that fetches DataRows for aDbEntity
determined from provided "dbEntityName" and uses provided expression for its qualifier.distinct()
entityName
(String entityName) Sets theObjEntity
name to fetch without changing the return type of the query.entityType
(Class<?> entityType) Sets the type of the entity to fetch without changing the return type of the query.Forces query to fetch DataRows.protected org.apache.cayenne.query.BaseQueryMetadata
getMetaData
(EntityResolver resolver) Returns default select 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.boolean
limit
(int fetchLimit) Resets query fetch limit - a parameter that defines max number of objects that should be ever be fetched from the database.Instructs Cayenne to look for query results in the "local" cache when running the query.localCache
(String cacheGroup) Instructs Cayenne to look for query results in the "local" cache when running the 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 propertyoffset
(int fetchOffset) Resets query fetch offset - a parameter that defines how many objects should be skipped when reading data from the database.or
(Collection<Expression> expressions) OR's provided expressions to the existing WHERE clause expression.or
(Expression... expressions) OR's provided expressions to the existing WHERE clause expression.Add an ascending ordering on the given property.Add an ordering on the given property.orderBy
(Collection<Ordering> orderings) Adds a list of orderings to this query.Add one or more orderings to this query.pageSize
(int pageSize) Resets query page size.Merges a prefetch path with specified semantics into the query prefetch tree.prefetch
(PrefetchTreeNode prefetch) Merges prefetch into the query prefetch tree.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 forObjEntity
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.queryTimeout
(int timeout) Sets query timeout for PreparedStatement generated for this query.long
selectCount
(ObjectContext context) Quick way to select count of recordsselectFirst
(ObjectContext context) Selects a single object using provided context.Instructs Cayenne to look for query results in the "shared" cache when running the query.sharedCache
(String cacheGroup) Instructs Cayenne to look for query results in the "shared" cache when running the query.statementFetchSize
(int size) Sets fetch size of the PreparedStatement generated for this query.<E extends Number>
ColumnSelect<E> sum
(NumericProperty<E> property) Select sum of values<E extends Number>
ColumnSelect<E> Deprecated.since 4.2 usesum(NumericProperty)
Appends a qualifier expression of this query, using provided expression String and an array of position parameters.where
(Expression expression) Appends a qualifier expression of this query.Methods inherited from class org.apache.cayenne.query.FluentSelect
batchIterator, createSQLAction, getColumns, getDbEntityName, getEntityName, getEntityType, getHaving, getLimit, getOffset, getOrderings, getPageSize, getPrefetches, getQueryTimeout, getStatementFetchSize, getWhere, initWithProperties, isDistinct, iterate, iterator, resolveRoot, route, routePrefetches, select, selectOne
Methods inherited from class org.apache.cayenne.query.AbstractQuery
getRoot, setRoot, toString
Methods inherited from class org.apache.cayenne.query.CacheableQuery
getCacheGroup, getCacheStrategy, setCacheGroup, setCacheStrategy, useLocalCache, useLocalCache, useSharedCache, useSharedCache
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.cayenne.query.Query
createSQLAction, route
-
Field Details
-
metaData
protected org.apache.cayenne.query.ObjectSelectMetadata metaData
-
-
Constructor Details
-
ObjectSelect
protected ObjectSelect()
-
-
Method Details
-
query
Creates a ObjectSelect that selects objects of a given persistent class. -
query
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
Creates a ObjectSelect that fetches data for anObjEntity
determined from a provided class. -
dataRowQuery
Creates a ObjectSelect that fetches data for anObjEntity
determined from a provided class and uses provided expression for its qualifier. -
query
Creates a ObjectSelect that fetches data forObjEntity
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
Creates a ObjectSelect that fetches DataRows for aDbEntity
determined from provided "dbEntityName". -
dbQuery
Creates a ObjectSelect that fetches DataRows for aDbEntity
determined from provided "dbEntityName" and uses provided expression for its qualifier.- Returns:
- this object
-
columnQuery
Creates a ColumnSelect that will fetch single property that can be resolved against a givenObjEntity
class.- 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
-
entityType
Sets the type of the entity to fetch without changing the return type of the query.- Returns:
- this object
-
entityName
Sets theObjEntity
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
Sets theDbEntity
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
Appends a qualifier expression of this query. An equivalent toand(Expression...)
that can be used a syntactic sugar.- Returns:
- this object
-
where
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
-
having
Appends a having qualifier expression of this query. An equivalent toand(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
-
and
AND's provided expressions to the existing WHERE clause expression.- Returns:
- this object
-
and
AND's provided expressions to the existing WHERE clause expression.- Returns:
- this object
-
or
OR's provided expressions to the existing WHERE clause expression.- Returns:
- this object
-
or
OR's provided expressions to the existing WHERE clause expression.- Returns:
- this object
-
orderBy
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
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 onsortOrder
- the direction of the ordering- Returns:
- this object
-
orderBy
Add one or more orderings to this query.- Returns:
- this object
-
orderBy
Adds a list of orderings to this query.- Returns:
- this object
-
prefetch
Merges prefetch into the query prefetch tree.- Returns:
- this object
-
prefetch
Merges a prefetch path with specified semantics into the query prefetch tree.- Returns:
- this object
-
limit
Resets query fetch limit - a parameter that defines max number of objects that should be ever be fetched from the database. -
offset
Resets query fetch offset - a parameter that defines how many objects should be skipped when reading data from the database. -
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
Sets fetch size of the PreparedStatement generated for this query. Only non-negative values would change the default size.- See Also:
-
queryTimeout
Sets query timeout for PreparedStatement generated for this query.- Since:
- 4.2
- See Also:
-
cacheStrategy
-
cacheStrategy
-
cacheGroup
-
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, cacheGroup);
-
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);
-
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
Select COUNT(*)- See Also:
-
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
Deprecated.since 4.2 useavg(NumericProperty)
Select average value of property
- See Also:
-
avg
Select average value of property
- See Also:
-
sum
Deprecated.since 4.2 usesum(NumericProperty)
Select sum of values
- See Also:
-
sum
Select sum of values
- 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: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 interfaceSelect<T>
-
isFetchingDataRows
public boolean isFetchingDataRows()- Overrides:
isFetchingDataRows
in classFluentSelect<T>
-
getMetaData
Description copied from class:AbstractQuery
Returns default select parameters.- Specified by:
getMetaData
in interfaceQuery
- Overrides:
getMetaData
in classAbstractQuery
-
getBaseMetaData
protected org.apache.cayenne.query.BaseQueryMetadata getBaseMetaData()- Specified by:
getBaseMetaData
in classCacheableQuery
-
createQuery
This method is intended for internal use in aMappedSelect
.- Specified by:
createQuery
in interfaceParameterizedQuery
- Parameters:
parameters
- to apply- Returns:
- this query with parameters applied to the where qualifier
- Since:
- 4.2
-
avg(NumericProperty)