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> 173<br />

match the bean field names in the EmployeeBean above. <strong>pureQuery</strong> does the work to<br />

map the result columns to the correct bean fields.<br />

SELECT EMPLOYEE.EMPNO, EMPLOYEE.FIRSTNME, EMPLOYEE.LASTNAME,<br />

DEPARTMENT.DEPTNO FROM EMPLOYEE LEFT JOIN DEPARTMENT ON EMPLOYEE.WORKDEPT<br />

= DEPARTMENT.DEPTNO<br />

Listing 9.2- An example SQL query to populate the EmployeeBean.<br />

A <strong>pureQuery</strong> bean is a Java class that defines a set of properties. Each property is<br />

defined either by a public field, or by a pair of public accessor methods. The following rules<br />

define how <strong>pureQuery</strong> determines the properties in a bean.<br />

• Public field: The name of the field indicates the name of the property. For example,<br />

a field defined as public String employeeNumber would represent a property<br />

named “employeeNumber”.<br />

• Public accessor methods: Each property defined by accessor methods must have<br />

one setter accessor method and one getter accessor method. Any setter method<br />

in a <strong>pureQuery</strong> bean must have a corresponding getter method, and vice versa,<br />

Setter and getter methods are defined according to these rules:<br />

o<br />

o<br />

Setter methods: The names of setter methods start with “set”. These<br />

methods have exactly one parameter. A setter method named<br />

“setAbcDef” indicates a property named “abcDef.” For example, a bean<br />

could contain a method that is defined as: public void<br />

setEmployeeNumber(String);. Such a method would indicate a<br />

property named “employeeBean.”<br />

Getter methods: The names of getter methods start with “get” or “is”.<br />

These methods have no parameters. A getter method named “getAbcDef”<br />

indicates a property named “abcDef,” as does a getter method named<br />

“isAbcDef” when property “abcDef” is of Boolean type. For example, a<br />

bean could contain a method that is defined as: public String<br />

getEmployeeNumber();. Such a method would indicate a property<br />

named “employeeNumber.”<br />

The names of the properties in a <strong>pureQuery</strong> bean must be unique in a case-insensitive<br />

search. Therefore, for example, if you have a pair of public accessor methods named<br />

setEmployeeNumber and getEmployeeNumber, you could not also have a pair of<br />

public accessor methods named setEmployeenumber and getEmployeenumber. You<br />

also could not have a public field named employeenumber.<br />

Every <strong>pureQuery</strong> bean must have a no-argument constructor, either explicit or implicit.<br />

Additionally, the type of each property must be supported by JDBC.<br />

By default, <strong>pureQuery</strong> maps a <strong>pureQuery</strong> bean property to a database column with the<br />

same name or alias. You can use Java annotations to change these mappings. The<br />

annotations are as follows:<br />

• @Column

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

Saved successfully!

Ooh no, something went wrong!