16.10.2015 Views

Getting Startedwith pureQuery

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 – Persistence Frameworks and Data Access: Context for <strong>pureQuery</strong> 177<br />

See Chapter 13 for more information about implementing custom handlers.<br />

9.3.3 Query over Collections with <strong>pureQuery</strong><br />

<strong>pureQuery</strong> supports transparent, mixed, in-memory vs. relational queries using standard<br />

SQL. This feature is called Query over Collections, and allows the object-relational layer to<br />

be implemented using a hybrid approach dividing query processing between a traditional<br />

relational database backend and in-memory objects.<br />

Depending on the kind of processing that is required, there may be systems which can be<br />

optimized by pulling a large, initial collection of data from the database using a traditional<br />

database query, then closing the database connection and performing several further,<br />

filtering queries on the initial collection in-memory. The advantage of this scheme is that<br />

the initial database connection resources can be freed up for other clients. Likewise, if the<br />

same set of initial data will have a number of filtering queries performed on it, the ability to<br />

query over the initial collection in-memory cuts the cost of additional trips to the database<br />

for each subsequent sub-query.<br />

To query over collections, <strong>pureQuery</strong> accepts a parameter marker in the place of the table<br />

name in any SQL query using SQL-92 standard syntax. The application then provides a<br />

java collection for the table parameter to the annotated or inline <strong>pureQuery</strong> method which<br />

performs the query.<br />

In Listing 3.4, a java List containing the contents of a previous database query is provided<br />

as a parameter to a <strong>pureQuery</strong> query-over-collection annotated method. In this example,<br />

the <strong>pureQuery</strong> numbered parameter marker (“?1”) represents a table in the SQL query<br />

syntax. <strong>pureQuery</strong>’s implementation for this method will treat the List of Customers as the<br />

table over which to perform the query.<br />

import com.company.Customer;<br />

import com.company.Campaign.CitySize;<br />

public interface RegionReport<br />

{<br />

@Select(sql="SELECT COUNT(DISTINCT city) FROM ?1")<br />

public Integer countCities (List curRegion);<br />

}<br />

Listing 9.4 - An example of a Query Over Collection, in this case a List of Customers.<br />

9.3.4 XML entity mapping with <strong>pureQuery</strong><br />

One common way to increase the flexibility of the object-relational mapping layer is to<br />

separate the object-relational mapping definition from the application code. <strong>pureQuery</strong><br />

supports XML entity-mapping files for annotated methods defined for <strong>pureQuery</strong> data<br />

access objects. See Chapter 11 to learn how to create <strong>pureQuery</strong> data access objects with<br />

user-defined, annotated methods using a <strong>pureQuery</strong> development project. To use XML<br />

entity mapping with <strong>pureQuery</strong>, an XML object-relational mapping file is provided to the<br />

<strong>pureQuery</strong> tooling along with the <strong>pureQuery</strong> annotated interface used to generate the class

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!