blob: b2a93b8497ddf5132305623f66f1e9d5df7bdbd7 [file] [log] [blame]
Title: Running Queries
<P>Cayenne Query API is discussed in the following chapters. This chapter concentrates on how to execute an existing query via a DataContext. Still we'd like to point to a few aspects of the query API here, as they are important for the query execution discussion:</P>
<UL>
<LI>There are selecting and non-selecting queries. Selecting queries return objects back to the caller, while non-selecting just perform some DB operation (such as INSERT, DELETE, UPDATE, etc.) without returning anything back.</LI>
<LI>Queries can be created in two ways - either by instantiating and configuring a Query object directly, or by mapping a query in CayenneModeler, and then referring to it by name. Mapped named queries are generally easier to create and maintain.</LI>
</UL>
<P>Being an instance of QueryEngine, DataContext has generic API to execute any type of queries, however it is rarely used directly. Instead DataContext provides a number of overriden convenience methods to run the types of queries mentioned above.</P>
<H3><A name="RunningQueries-RunningQueryInstances"></A>Running Query Instances</H3>
<P>The following methods are used for running selecting and non-selecting Query objects built in the code:</P>
<UL>
<LI><TT>public java.util.List performQuery(Query query)</TT><BR>
This is the most commonly used method for any selecting query.</LI>
</UL>
<UL>
<LI><TT>public ResultIterator performIteratedQuery(Query query) throws CayenneException</TT><BR>
Performs a selecting query, returning an iterator over the result backed by an open JDBC ResultSet. Benefits and shortcomings of using result iterators are discussed in &quot;Performance Tuning&quot; chapter. This is the only query method that throws checked exceptions. Also returned ResultIterator requires explicit cleanup via calling close().</LI>
</UL>
<UL>
<LI><TT>public int[] performNonSelectingQuery(Query query)</TT><BR>
Executes a non-selecting query, returning update counts.</LI>
</UL>
<UL>
<LI><TT>public QueryResponse performGenericQuery(Query query)</TT><BR>
A generic query execution method. Used for queries with complex results. For example queries based on stored procedures can have more than one result set, do multiple updates, and return values as OUT parameters.</LI>
</UL>
<H3><A name="RunningQueries-RunningMappedQueries"></A>Running Mapped Queries</H3>
<P>The following methods are used to run named selecting and non-selecting queries mapped using CayenneModeler and stored in the DataMap XML file:</P>
<UL>
<LI><TT>public java.util.List performQuery(String name, boolean expireCachedLists)</TT><BR>
Runs a named selecting query stored in the DataMap.</LI>
</UL>
<UL>
<LI><TT>public java.util.List performQuery(String name, Map parameters, boolean expireCachedLists)</TT><BR>
Runs a named selecting query stored in the DataMap, using a map of named parameters.</LI>
</UL>
<UL>
<LI><TT>public int[] performNonSelectingQuery(String name)</TT><BR>
Runs a named non-selecting query stored in the DataMap.</LI>
</UL>
<UL>
<LI><TT>public int[] performNonSelectingQuery(String name, Map parameters)</TT><BR>
Runs a named non-selecting query stored in the DataMap, using a map of named parameters.</LI>
</UL>