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.

220<br />

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

We are executing a SELECT statement, so we use the<br />

com.ibm.pdq.annotation.Select annotation. Since we need several pieces of<br />

information about each employee, it would be convenient to retrieve each row as an<br />

instance of EmployeeBean. We will be selecting all the employees in the database, but<br />

we would prefer not to have to store all of the company's employees in the application’s<br />

memory at once, since that could be quite a lot of memory, so we will return the rows in a<br />

java.util.Iterator object. Therefore, we will use<br />

java.util.Iterator as the return type of our method.<br />

Our SQL statement does not have any parameters, so the method does not need any<br />

parameters.<br />

While users are viewing the list of employees, they can request to view additional<br />

information about an employee and then return to the list. Therefore, we need to set the<br />

cursor holdability to “hold cursors over commit.” We use the<br />

com.ibm.pdq.annotation.Cursor annotation to specify the holdability cursor attribute<br />

java.sql.ResultSet.HOLD_CURSORS_OVER_COMMIT. Listing 11.9 shows the<br />

definition of the new annotated method.<br />

@Cursor(holdability = ResultSet.HOLD_CURSORS_OVER_COMMIT)<br />

@Select(sql = "SELECT * FROM EMPLOYEE ORDER BY LASTNAME, FIRSTNME,<br />

MIDINIT")<br />

Iterator selectAllEmployees ();<br />

Listing 11.9 - Add an annotated method to the HumanResourcesData interface that<br />

creates an Iterator of all the employees<br />

After you have added the method to<br />

hrm.inProgress.pdqMethodStyle.HumanResourcesData, build your project so that<br />

Optim Development Studio will generate an implementation class. Confirm that<br />

hrm.inProgress.pdqMethodStyle.HumanResourcesDataImpl was generated<br />

without any problems. Figure 11.6 shows the location of the new file. If IBM Optim<br />

Development studio has placed a red exception marker on the interface, read the text of<br />

the exception and determine what you need to do to solve the problem.<br />

Figure 11.6 - The generated implementation class HumanResourcesDataImpl<br />

11.4.2.2 Modify the displayAllEmployees method to use the new annotated<br />

method<br />

In hrm.inProgress.pdqMethodStyle.HumanResourcesManager, modify the<br />

displayAllEmployees method to use the new annotated method to query the database

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

Saved successfully!

Ooh no, something went wrong!