10. Performing Complex Queries using JPQL

PROBLEM

The JPA specification defines the Java Persistence Query Language (JPQL) which is a standard mechanism for accessing data. Applications developed with Skyway Builder are automatically configured to use JPA, and the developer can leverage JPQL for accessing databases.

SOLUTION

A Named Query is a mechanism for predefining queries for the domain objects associated with a Data Access Object. Named queries are defined using either SQL or JPQL (Java Persistence Query Language). JPQL is the query lanaguage of JPA, that resembles SQL queries. A developer can add Named Queries to a data access object, and then use the Invoke Named Query step to use the queries from Actions.

The benefits of named queries is that the queries are centralized and they can more easily be re-used. Using the Modify Data Access Object and Search Data Access Object steps througout your application can make it more more difficult to manage where and how the data access is occurring because the queries are contained in the generated code. On the other hand named queries are all located with their associated data access object. This makes it a lot easier for developer to find the query for re-using, repurposing or optimizing.

HOW IT WORKS

Inputs and Outputs These define the input and output parameters of a particular named query. When this query is invoked, the consumer of the name query is required to map variables to the input and output parameters.

Query TextThis defines the query in either SQL or JPQL.

Table 4.3. JPA Query Examples

TypeQuery
Select
SELECT ln FROM LoanAccountInfo ln 
              WHERE ln.accountNumber = ${inAccountNumber}
Insert
insert into NamedQueryTest values(${key}, ${value})
Update
UPDATE NamedQueryTest t SET t.field_2 = ${value} 
              WHERE t.field_1 = ${key}
Delete
DELETE FROM NamedQueryTest c 
              WHERE c.field_1 = ${inKey}