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.

Chapter 15 – <strong>pureQuery</strong> and pureXML 289<br />

expression will be used to update the correct nodes in the original XML document. Listing<br />

15.2 shows an example of an XQuery Transform expression that can be used to update<br />

the Employee information in the database:<br />

public static void updateEmployee(Data data, Employee emp){<br />

String sql = "update employee set doc = xmlquery(" +<br />

"'copy $new := $DOC " +<br />

"modify (do replace value of $new/employee/name/first with<br />

$firstname," +<br />

"do replace value of $new/employee/name/last with $lastname," +<br />

"do replace value of $new/employee/office with $office)" +<br />

"return $new' " +<br />

"passing cast(:firstname as varchar(20)) as \"firstname\", " +<br />

"cast(:lastname as varchar(20)) as \"lastname\"," +<br />

"cast(:office as INTEGER) as \"office\"" +<br />

") where id = :id";<br />

data.update(sql, emp);<br />

}<br />

Listing 15.2 – Using an XQuery Transform expression<br />

Using this approach allows the application developers to be oblivious of schema evolution<br />

in the application data, as the mapping is done in the database, hiding these changes from<br />

the application.<br />

15.3 Give control to the data access API<br />

In this approach, we will show how XML data can be mapped into Java objects and viceversa<br />

using the data access API, instead of doing this mapping at the database level like in<br />

the previous approach.<br />

The <strong>pureQuery</strong> API provides several plug-in mechanisms, including custom result set and<br />

custom data row handlers. These custom handlers provide you with means to override<br />

default mapping strategies from relational data into Java beans. The ResultHandler lets<br />

you control the mapping strategy for the entire result set. This is very useful for aggregation<br />

queries and one-to-many relationships, when several consecutive result sets may contain<br />

information about the same business object. The RowHandler lets you define how<br />

individual relational result set rows are mapped into a Java bean. Unlike the ResultHandler,<br />

the RowHandler only has access to the current row in the result set and not the entire<br />

result set.<br />

By taking advantage of these plug-in mechanisms, you can use one of the several existing<br />

Java-XML mapping frameworks to map from XML documents stored as pureXML data into<br />

your business logic Java beans, as shown in Figure 15.2.

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

Saved successfully!

Ooh no, something went wrong!