Class SelectQuery<T>
- All Implemented Interfaces:
- Serializable,- ParameterizedQuery,- Query,- Select<T>
- Direct Known Subclasses:
- PrefetchSelectQuery
public class SelectQuery<T> extends AbstractQuery implements ParameterizedQuery, Select<T>
- See Also:
- Serialized Form
- 
Field SummaryFields Modifier and Type Field Description protected booleancanReturnScalarValueFlag that indicates whether this query can return single value or it should always return some complex data (Object[] for now)protected Collection<Property<?>>columnsprotected booleandistinctstatic booleanDISTINCT_DEFAULTstatic StringDISTINCT_PROPERTYprotected ExpressionhavingQualifierprotected List<Ordering>orderingsprotected ExpressionqualifierFields inherited from class org.apache.cayenne.query.AbstractQueryrootFields inherited from class org.apache.cayenne.query.CacheableQuerylogger
- 
Constructor SummaryConstructors Constructor Description SelectQuery()Creates an empty SelectQuery.SelectQuery(Class<T> rootClass)Creates a SelectQuery that selects all objects of a given persistent class.SelectQuery(Class<T> rootClass, Expression qualifier)Creates a SelectQuery that selects objects of a given persistent class that match supplied qualifier.SelectQuery(Class<T> rootClass, Expression qualifier, List<? extends Ordering> orderings)Creates a SelectQuery that selects objects of a given persistent class that match supplied qualifier.SelectQuery(String objEntityName)Creates SelectQuery withobjEntityNameparameter.SelectQuery(String objEntityName, Expression qualifier)Creates SelectQuery withobjEntityNameandqualifierparameters.SelectQuery(String objEntityName, Expression qualifier, List<? extends Ordering> orderings)Creates a SelectQuery that selects objects of a given persistent class that match supplied qualifier.SelectQuery(DbEntity root)Creates a SelectQuery for the specified DbEntity.SelectQuery(DbEntity root, Expression qualifier)Creates a SelectQuery for the specified DbEntity with the given qualifier.SelectQuery(DbEntity root, Expression qualifier, List<? extends Ordering> orderings)Creates a SelectQuery for the specified DbEntity with the given qualifier and orderings.SelectQuery(ObjEntity root)Creates a SelectQuery with null qualifier, for the specifed ObjEntitySelectQuery(ObjEntity root, Expression qualifier)Creates a SelectQuery for the specified ObjEntity with the given qualifier.SelectQuery(ObjEntity root, Expression qualifier, List<? extends Ordering> orderings)Creates a SelectQuery for the specified ObjEntity with the given qualifier and orderings.
- 
Method SummaryModifier and Type Method Description voidaddOrdering(String sortPathSpec, SortOrder order)Adds ordering specification to this query orderings.voidaddOrdering(Ordering ordering)Adds ordering specification to this query orderings.voidaddOrderings(Collection<? extends Ordering> orderings)Adds a list of orderings.PrefetchTreeNodeaddPrefetch(String prefetchPath)Adds a prefetch with specified relationship path to the query.voidaddPrefetch(PrefetchTreeNode prefetchElement)Adds a prefetch with specified relationship path to the query.voidaliasPathSplits(String path, String... aliases)Adds one or more aliases for the qualifier expression path.voidandHavingQualifier(Expression e)Adds specified HAVING qualifier to the existing HAVING qualifier joining it using "AND".voidandQualifier(Expression e)Adds specified qualifier to the existing qualifier joining it using "AND".ResultBatchIterator<T>batchIterator(ObjectContext context, int size)Creates a ResultBatchIterator based on the provided context and batch size.booleancanReturnScalarValue()voidclearOrderings()Clears all configured orderings.voidclearPrefetches()Clears all stored prefetch paths.SelectQuery<T>createQuery(Map<String,?> parameters)Creates and returns a new SelectQuery built using this query as a prototype and substituting qualifier parameters with the values from the map.SQLActioncreateSQLAction(SQLActionVisitor visitor)Calls "makeSelect" on the visitor.static SelectQuery<DataRow>dataRowQuery(Class<?> rootClass)static SelectQuery<DataRow>dataRowQuery(Class<?> rootClass, Expression qualifier)Creates a SelectQuery that selects DataRows that correspond to a given persistent class that match supplied qualifier.static SelectQuery<DataRow>dataRowQuery(Class<?> rootClass, Expression qualifier, List<Ordering> orderings)protected org.apache.cayenne.query.BaseQueryMetadatagetBaseMetaData()Collection<Property<?>>getColumns()intgetFetchLimit()Returns the fetchLimit.intgetFetchOffset()Returns the fetchOffset.ExpressiongetHavingQualifier()Returns query HAVING qualifier.QueryMetadatagetMetaData(EntityResolver resolver)Returns default select parameters.List<Ordering>getOrderings()Returns a list of orderings used by this query.intgetPageSize()ReturnspageSizeproperty.PrefetchTreeNodegetPrefetchTree()ExpressiongetQualifier()Returns query qualifier.intgetStatementFetchSize()voidinitWithProperties(Map<String,?> properties)Initializes query parameters using a set of properties.booleanisDistinct()Returns true if this query returns distinct rows.booleanisFetchingDataRows()Returnstrueif this query should produce a list of data rows as opposed to DataObjects,falsefor DataObjects.voiditerate(ObjectContext context, ResultIteratorCallback<T> callback)Creates a ResultIterator based on the provided context and passes it to a callback for processing.ResultIterator<T>iterator(ObjectContext context)Creates a ResultIterator based on the provided context.voidorHavingQualifier(Expression e)Adds specified HAVING qualifier to the existing HAVING qualifier joining it using "OR".voidorQualifier(Expression e)Adds specified qualifier to the existing qualifier joining it using "OR".static <T> SelectQuery<T>query(Class<T> rootClass)Creates a SelectQuery that selects objects of a given persistent class.static <T> SelectQuery<T>query(Class<T> rootClass, Expression qualifier)Creates a SelectQuery that selects objects of a given persistent class that match supplied qualifier.static <T> SelectQuery<T>query(Class<T> rootClass, Expression qualifier, List<? extends Ordering> orderings)Creates a SelectQuery that selects objects of a given persistent class that match supplied qualifier.SelectQuery<T>queryWithParameters(Map<String,?> parameters)A shortcut forqueryWithParameters(Map, boolean)that prunes parts of qualifier that have no parameter value set.SelectQuery<T>queryWithParameters(Map<String,?> parameters, boolean pruneMissing)Returns a query built using this query as a prototype, using a set of parameters to build the qualifier.voidremoveOrdering(Ordering ordering)Removes ordering.voidremovePrefetch(String prefetchPath)Removes prefetch.voidroute(QueryRouter router, EntityResolver resolver, Query substitutedQuery)Routes itself and if there are any prefetches configured, creates prefetch queries and routes them as well.List<T>select(ObjectContext context)Selects objects using provided context.TselectFirst(ObjectContext context)Selects a single object using provided context.TselectOne(ObjectContext context)Selects a single object using provided context.voidsetCanReturnScalarValue(boolean canReturnScalarValue)Flag that indicates whether this query can return single value or it should always return some complex data (Object[] for now)voidsetColumns(Collection<Property<?>> columns)voidsetColumns(Property<?>... columns)voidsetDistinct(boolean distinct)Setsdistinctproperty that determines whether this query returns distinct row.voidsetFetchingDataRows(boolean flag)Deprecated.voidsetFetchLimit(int fetchLimit)Sets the fetchLimit.voidsetFetchOffset(int fetchOffset)voidsetHavingQualifier(Expression qualifier)Sets new query HAVING qualifier.voidsetPageSize(int pageSize)SetspageSizeproperty.voidsetPrefetchTree(PrefetchTreeNode prefetchTree)voidsetQualifier(Expression qualifier)Sets new query qualifier.voidsetStatementFetchSize(int size)Sets statement's fetch size (0 for default size)voidsetSuppressDistinct(boolean suppressDistinct)Setsdistinctproperty that determines whether this query returns distinct row.Methods inherited from class org.apache.cayenne.query.AbstractQuerygetRoot, setRoot, toStringMethods inherited from class org.apache.cayenne.query.CacheableQuerygetCacheGroup, getCacheStrategy, setCacheGroup, setCacheStrategy, useLocalCache, useLocalCache, useSharedCache, useSharedCache
- 
Field Details- 
DISTINCT_PROPERTY- See Also:
- Constant Field Values
 
- 
DISTINCT_DEFAULTpublic static final boolean DISTINCT_DEFAULT- See Also:
- Constant Field Values
 
- 
qualifier
- 
orderings
- 
distinctprotected boolean distinct
- 
columns- Since:
- 4.0
 
- 
havingQualifier- Since:
- 4.0
 
- 
canReturnScalarValueprotected boolean canReturnScalarValueFlag that indicates whether this query can return single value or it should always return some complex data (Object[] for now) Default value is true - Since:
- 4.0
 
 
- 
- 
Constructor Details- 
SelectQuerypublic SelectQuery()Creates an empty SelectQuery.
- 
SelectQueryCreates a SelectQuery with null qualifier, for the specifed ObjEntity- Parameters:
- root- the ObjEntity this SelectQuery is for.
 
- 
SelectQueryCreates a SelectQuery for the specified ObjEntity with the given qualifier.- Parameters:
- root- the ObjEntity this SelectQuery is for.
- qualifier- an Expression indicating which objects should be fetched
 
- 
SelectQueryCreates a SelectQuery for the specified ObjEntity with the given qualifier and orderings.- Parameters:
- root- the ObjEntity this SelectQuery is for.
- qualifier- an Expression indicating which objects should be fetched.
- orderings- defines how to order the results, may be null.
- Since:
- 3.1
 
- 
SelectQueryCreates a SelectQuery that selects all objects of a given persistent class.- Parameters:
- rootClass- the Class of objects fetched by this query.
 
- 
SelectQueryCreates a SelectQuery that selects objects of a given persistent class that match supplied qualifier.- Parameters:
- rootClass- the Class of objects fetched by this query.
- qualifier- an Expression indicating which objects should be fetched.
 
- 
SelectQueryCreates a SelectQuery that selects objects of a given persistent class that match supplied qualifier.- Parameters:
- rootClass- the Class of objects fetched by this query.
- qualifier- an Expression indicating which objects should be fetched.
- orderings- defines how to order the results, may be null.
- Since:
- 3.1
 
- 
SelectQueryCreates a SelectQuery for the specified DbEntity.- Parameters:
- root- the DbEntity this SelectQuery is for.
- Since:
- 1.1
 
- 
SelectQueryCreates a SelectQuery for the specified DbEntity with the given qualifier.- Parameters:
- root- the DbEntity this SelectQuery is for.
- qualifier- an Expression indicating which objects should be fetched.
- Since:
- 1.1
 
- 
SelectQueryCreates a SelectQuery for the specified DbEntity with the given qualifier and orderings.- Parameters:
- root- the DbEntity this SelectQuery is for.
- qualifier- an Expression indicating which objects should be fetched.
- orderings- defines how to order the results, may be null.
- Since:
- 3.1
 
- 
SelectQueryCreates SelectQuery withobjEntityNameparameter.
- 
SelectQueryCreates SelectQuery withobjEntityNameandqualifierparameters.
- 
SelectQueryCreates a SelectQuery that selects objects of a given persistent class that match supplied qualifier.- Parameters:
- objEntityName- the name of the ObjEntity to fetch from.
- qualifier- an Expression indicating which objects should be fetched.
- orderings- defines how to order the results, may be null.
- Since:
- 3.1
 
 
- 
- 
Method Details- 
queryCreates a SelectQuery that selects objects of a given persistent class.- Parameters:
- rootClass- the Class of objects fetched by this query.
- Since:
- 4.0
 
- 
queryCreates a SelectQuery that selects objects of a given persistent class that match supplied qualifier.- Parameters:
- rootClass- the Class of objects fetched by this query.
- qualifier- an Expression indicating which objects should be fetched.
- Since:
- 4.0
 
- 
querypublic static <T> SelectQuery<T> query(Class<T> rootClass, Expression qualifier, List<? extends Ordering> orderings)Creates a SelectQuery that selects objects of a given persistent class that match supplied qualifier.- Parameters:
- rootClass- the Class of objects fetched by this query.
- qualifier- an Expression indicating which objects should be fetched.
- orderings- defines how to order the results, may be null.
- Since:
- 4.0
 
- 
dataRowQuery- Since:
- 4.0
 
- 
dataRowQueryCreates a SelectQuery that selects DataRows that correspond to a given persistent class that match supplied qualifier.- Parameters:
- rootClass- the Class of objects that correspond to DataRows entity.
- qualifier- an Expression indicating which objects should be fetched.
- Since:
- 4.0
 
- 
dataRowQuerypublic static SelectQuery<DataRow> dataRowQuery(Class<?> rootClass, Expression qualifier, List<Ordering> orderings)- Since:
- 4.0
 
- 
selectDescription copied from interface:SelectSelects objects using provided context.Essentially the inversion of "ObjectContext.select(Select)". 
- 
selectOneDescription copied from interface:SelectSelects a single object using provided context. The query is expected to match zero or one object. It returns null if no objects were matched. If query matched more than one object,CayenneRuntimeExceptionis thrown.Essentially the inversion of "ObjectContext.selectOne(Select)". 
- 
selectFirstDescription 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. - Specified by:
- selectFirstin interface- Select<T>
 
- 
iterateDescription copied from interface:SelectCreates a ResultIterator based on the provided context and passes it to a callback for processing. The caller does not need to worry about closing the iterator. This method takes care of it.Essentially the inversion of "ObjectContext.iterate(Select, ResultIteratorCallback)". 
- 
iteratorDescription copied from interface:SelectCreates a ResultIterator based on the provided context. It is usually backed by an open result set and is useful for processing of large data sets, preserving a constant memory footprint. The caller must wrap iteration in try/finally (or try-with-resources for Java 1.7 and higher) and close the ResultIterator explicitly. Or useSelect.iterate(ObjectContext, ResultIteratorCallback)as an alternative.Essentially the inversion of "ObjectContext.iterator(Select)". 
- 
batchIteratorDescription copied from interface:SelectCreates a ResultBatchIterator based on the provided context and batch size. It is usually backed by an open result set and is useful for processing of large data sets, preserving a constant memory footprint. The caller must wrap iteration in try/finally (or try-with-resources for Java 1.7 and higher) and close the ResultBatchIterator explicitly.- Specified by:
- batchIteratorin interface- Select<T>
 
- 
getMetaDataDescription copied from class:AbstractQueryReturns default select parameters.- Specified by:
- getMetaDatain interface- Query
- Overrides:
- getMetaDatain class- AbstractQuery
- Since:
- 1.2
 
- 
routeRoutes itself and if there are any prefetches configured, creates prefetch queries and routes them as well.- Specified by:
- routein interface- Query
- Overrides:
- routein class- AbstractQuery
- Since:
- 1.2
 
- 
createSQLActionCalls "makeSelect" on the visitor.- Specified by:
- createSQLActionin interface- Query
- Specified by:
- createSQLActionin class- AbstractQuery
- Since:
- 1.2
 
- 
initWithPropertiesInitializes query parameters using a set of properties.- Since:
- 1.1
 
- 
queryWithParametersA shortcut forqueryWithParameters(Map, boolean)that prunes parts of qualifier that have no parameter value set.
- 
queryWithParametersReturns a query built using this query as a prototype, using a set of parameters to build the qualifier.- See Also:
- parameter substitution.
 
- 
createQueryCreates and returns a new SelectQuery built using this query as a prototype and substituting qualifier parameters with the values from the map.- Specified by:
- createQueryin interface- ParameterizedQuery
- Since:
- 1.1
 
- 
addOrderingAdds ordering specification to this query orderings.
- 
addOrderingsAdds a list of orderings.
- 
addOrderingAdds ordering specification to this query orderings.- Since:
- 3.0
 
- 
removeOrderingRemoves ordering.- Since:
- 1.1
 
- 
getOrderingsReturns a list of orderings used by this query.
- 
clearOrderingspublic void clearOrderings()Clears all configured orderings.
- 
isDistinctpublic boolean isDistinct()Returns true if this query returns distinct rows.
- 
setDistinctpublic void setDistinct(boolean distinct)Setsdistinctproperty that determines whether this query returns distinct row.
- 
setSuppressDistinctpublic void setSuppressDistinct(boolean suppressDistinct)Setsdistinctproperty that determines whether this query returns distinct row.
- 
aliasPathSplitsAdds one or more aliases for the qualifier expression path. Aliases serve to instruct Cayenne to generate separate sets of joins for overlapping paths, that maybe needed for complex conditions. An example of an implicit splits is this method:ExpressionFactory.matchAllExp(String, Object...).- Since:
- 3.0
 
- 
getPrefetchTree- Since:
- 1.2
 
- 
setPrefetchTree- Since:
- 1.2
 
- 
addPrefetchAdds a prefetch with specified relationship path to the query.- Since:
- 4.0
 
- 
addPrefetchAdds a prefetch with specified relationship path to the query.- Since:
- 1.2 signature changed to return created PrefetchTreeNode.
 
- 
clearPrefetchespublic void clearPrefetches()Clears all stored prefetch paths.
- 
removePrefetchRemoves prefetch.- Since:
- 1.1
 
- 
isFetchingDataRowspublic boolean isFetchingDataRows()Returnstrueif this query should produce a list of data rows as opposed to DataObjects,falsefor DataObjects. This is a hint to QueryEngine executing this query.
- 
setFetchingDataRowspublic void setFetchingDataRows(boolean flag)Deprecated.since 4.0, usedataRowQuery(Class, Expression)to create DataRow query instead.Sets query result type. Ifflagparameter istrue, then results will be in the form of data rows.Note that if the root of this query is a DbEntity, this setting has no effect, and data rows are always fetched.
- 
getFetchOffsetpublic int getFetchOffset()Returns the fetchOffset.- Since:
- 3.0
 
- 
getFetchLimitpublic int getFetchLimit()Returns the fetchLimit.
- 
setFetchLimitpublic void setFetchLimit(int fetchLimit)Sets the fetchLimit.
- 
setFetchOffsetpublic void setFetchOffset(int fetchOffset)- Since:
- 3.0
 
- 
getPageSizepublic int getPageSize()ReturnspageSizeproperty. See setPageSize for more details.
- 
setPageSizepublic void setPageSize(int pageSize)SetspageSizeproperty. By setting a page size, the Collection returned by performing a query will return hollow DataObjects. This is considerably faster and uses a tiny fraction of the memory compared to a non-paged query when large numbers of objects are returned in the result. When a hollow DataObject is accessed all DataObjects on the same page will be faulted into memory. There will be a small delay when faulting objects while the data is fetched from the data source, but otherwise you do not need to do anything special to access data in hollow objects. The first page is always faulted into memory immediately.- Parameters:
- pageSize- The pageSize to set
 
- 
setStatementFetchSizepublic void setStatementFetchSize(int size)Sets statement's fetch size (0 for default size)- Since:
- 3.0
 
- 
getStatementFetchSizepublic int getStatementFetchSize()- Returns:
- statement's fetch size
- Since:
- 3.0
 
- 
setQualifierSets new query qualifier.
- 
getQualifierReturns query qualifier.
- 
andQualifierAdds specified qualifier to the existing qualifier joining it using "AND".
- 
orQualifierAdds specified qualifier to the existing qualifier joining it using "OR".
- 
setColumns- Since:
- 4.0
- See Also:
- setCanReturnScalarValue(boolean)
 
- 
setColumns- Since:
- 4.0
 
- 
setCanReturnScalarValuepublic void setCanReturnScalarValue(boolean canReturnScalarValue)Flag that indicates whether this query can return single value or it should always return some complex data (Object[] for now) Default value is true - Parameters:
- canReturnScalarValue- can this query return single value
- Since:
- 4.0
- See Also:
- setColumns(java.util.Collection<org.apache.cayenne.exp.Property<?>>)
 
- 
canReturnScalarValuepublic boolean canReturnScalarValue()- Returns:
- can this query return single value
- Since:
- 4.0
 
- 
getColumns- Since:
- 4.0
 
- 
setHavingQualifierSets new query HAVING qualifier.- Since:
- 4.0
 
- 
getHavingQualifierReturns query HAVING qualifier.- Since:
- 4.0
 
- 
andHavingQualifierAdds specified HAVING qualifier to the existing HAVING qualifier joining it using "AND".- Since:
- 4.0
 
- 
orHavingQualifierAdds specified HAVING qualifier to the existing HAVING qualifier joining it using "OR".- Since:
- 4.0
 
- 
getBaseMetaDataprotected org.apache.cayenne.query.BaseQueryMetadata getBaseMetaData()- Specified by:
- getBaseMetaDatain class- CacheableQuery
 
 
- 
dataRowQuery(Class, Expression)to create DataRow query instead.