16.10.2015 Views

Getting Startedwith pureQuery

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

282<br />

<strong>Getting</strong> Started with <strong>pureQuery</strong><br />

• Have predefined queries<br />

Use <strong>pureQuery</strong> inline style if you:<br />

• Like to have your SQL statements inline in your Java code, just like regular JDBC<br />

programming<br />

• Have dynamically generated queries<br />

As a rule of thumb, we recommend using the annotated method stule. Due to its flexibility<br />

and isolation from the business logic, the annotated method style simplifies tasks like<br />

refactoring, because code is in a single place, and code reutilization, by sharing your data<br />

access interfaces between projects.<br />

Keep in mind that you can also mix both programming styles in your application if your<br />

requirements cannot be met by a single style.<br />

14.3 Perform Joins with Table collation<br />

In addition to the basic data access generated by <strong>pureQuery</strong> tooling for a database table,<br />

sometimes you need to run a more complex query that joins two tables, and return<br />

information from both tables as Java beans.<br />

Table collation in <strong>pureQuery</strong> lets you reuse the generated beans in these types of queries,<br />

returning multiple beans for each result row.<br />

Consider the SQL statement in Listing 14.1, which shows a query that joins data from the<br />

tables PROJECT and DEPARTMENT:<br />

SELECT PROJECT.PROJNO, DEPARTMENT.DEPTNO<br />

FROM PROJECT, DEPARTMENT<br />

WHERE PROJECT.DEPTNO = DEPARTMENT.DEPTNO<br />

Listing 14.1 - Selecting data from the tables PROJECT and DEPARTMENT<br />

By using <strong>pureQuery</strong>’s table collation feature, you can specify that the result of this query<br />

should be represented in the business layer using the Project and Department <strong>pureQuery</strong><br />

beans, which you can easily generate using tooling.<br />

In order to hold these beans in a result set, you need to define a generic class that can<br />

hold the two beans and be used as the <strong>pureQuery</strong> API method return type:<br />

public class ProjectAndDepartment extends LinkedList<br />

{}<br />

Listing 14.2 - Generic class to hold a pair of beans<br />

You can then use this class as the type for the Iterator or List returned by your data access<br />

API:

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

Saved successfully!

Ooh no, something went wrong!