The most commonly used query is SelectQuery. It is a descriptor that allows DataContext to fetch lists of DataObjects of the right type matching the specified criteria. SelectQuery together with the DataMap provides just enough information to the Cayenne runtime objects to build the right SQL SELECT statement and control various execution parameters.
A SelectQuery consists of a root object, qualifier expression and orderings list. Here is a logical correspondence of Cayenne SelectQuery parts and SQL constructs:
|Cayenne SelectQuery||SQL SELECT statement||Required|
|Qualifier Expression||WHERE clause||no|
|Orderings||ORDER BY clause||no|
The only required query part is root. Query root tells Cayenne what kind of objects to fetch. It can be one of the following:
- (most commonly used) Java class for the type of persistent objects in question.
- ObjEntity that provides the mapping for the class in question.
- A String that is an ObjEntity name.
SelectQuery provides constructors for all three types. For example:
Other components of the SelectQuery are discussed in the following sections.
As mentioned earlier, queries are executed via DataContext.performQuery(). For instance to fetch all Artists existing in the database the following code is used:
There is a special case when a query is run using DataContext.performIteratedQuery(). This is discussed in "Performance Tuning" chapter.