To facilitate reuse of queries users can assign symbolic names to them and store such named queries in a DataMap. Normally this is done by creating a query in CayenneModeler. Storing queries in a DataMap reduces the amount of code and speeds up query creation process.
This example shows how to get a shared instance of a stored query, and use it as a template for customized query.
DataContext context = ... // assume this exists // 1. lookup prototype // note a cast to SelectQuery... Generally DataMap can store any type of queries SelectQuery prototype = (SelectQuery) context.getEntityResolver().lookupQuery("MySelect"); // 2. customize query Map params = new HashMap(); params.put("aname", "Monet"); SelectQuery query = prototype.queryWithParameters(params); // 3. execute query List objects = context.performQuery(query);
DataContext supports running a named query with preset parameters directly:
DataContext context = ... // assume this exists // "false" indicates that a cached result should be used if available List objects = context.performQuery("MySelect", false);