Skip to content

Commit 5175f4d

Browse files
authored
Merge pull request #49 from OpenLiberty/feedback
Query annotation updates
2 parents 989f410 + b2026cf commit 5175f4d

File tree

2 files changed

+17
-37
lines changed

2 files changed

+17
-37
lines changed

README.adoc

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Packages.java
9494
include::finish/src/main/java/io/openliberty/guides/data/Packages.java[]
9595
----
9696

97-
A common pattern with respositories is to use the plural of the entity, so the repository you will be working with in this guide is called `Packages.java`. Repositories in Jakarta Data provide a simplified means for interacting with persistent data. They are annotated with the `@Repository` annotation. Jakarta Data provides several built in Repositories with common methods which can be extended with application specific queries. The [hotspot=CrudRepository file=1]`CrudRepository` interface provides methods for Create, Read, Update, and Delete (CRUD) operations.
97+
A common pattern with repositories is to use the plural of the entity, so the repository you will be working with in this guide is called `Packages.java`. Repositories in Jakarta Data provide a simplified means for interacting with persistent data. They are annotated with the `@Repository` annotation. Jakarta Data provides several built-in repositories with common methods which can be extended with application-specific queries. The [hotspot=CrudRepository file=1]`CrudRepository` interface provides methods for Create, Read, Update, and Delete (CRUD) operations.
9898

9999
[hotspot=CrudRepository file=1]`CrudRepository` provides two options for persisting entities, `insert` and `save`. The first option `insert` will persist an entity only if the entity does not already exist. The `save` option will persist an entity even if it already exists, allowing it to be used for updating entities as well. [hotspot=CrudRepository file=1]`CrudRepository` provides a `delete` method for removing entities.
100100

@@ -179,29 +179,19 @@ include::finish/src/main/java/io/openliberty/guides/data/Packages.java[]
179179
----
180180

181181
=== Sort
182-
In addition to the `@OrderBy` annotation it is possible to provide sorting at runtime. This is accomplished by adding a [hotspot=Sort file=0]`Sort` parameter to your query method:
182+
In addition to the `@OrderBy` annotation it is possible to provide sorting at runtime. This is accomplished by adding a [hotspot=Sort file=0]`Sort` parameter to your query method like in the [hotspot=Sort file=0]`sorted` method.
183183

184-
```java
185-
@Find
186-
List<Package> sorted(Sort<?> sortBy);
187-
```
188-
189-
A `Sort` can be created and provided at runtime using the `Sort` class's static methods:
184+
A [hotspot=Sort file=0]`Sort` can be created and provided at runtime using the [hotspot=Sort file=0]`Sort` class's static methods:
190185
```java
191186
Sort sort = Sort.desc("height");
192187
```
193188

194189
=== Limit
195-
Jakarta Data queries can restrict the number of entities returned at runtime by providing a [hotspot=Limit file=0]`Limit` object to the query:
190+
Jakarta Data queries can restrict the number of entities returned at runtime by providing a [hotspot=Limit file=0]`Limit` object to the query as shown in the [hotspot=Limit file=0]`shortestWithLimit` method.
196191

197-
```java
198-
@Find
199-
@OrderBy("length")
200-
List<Package> shortestWithLimit(Limit limit);
201-
```
202-
This method will return the shortest packages in order from shortest to longest, but limited only to only the specified number of results.
192+
This method will return the shortest packages in order from shortest to longest, but limited to only the specified number of results.
203193

204-
Limits can be created and supplied using the `Limit` class's static methods:
194+
Limits can be created and supplied using the [hotspot=Limit file=0]`Limit` class's static methods:
205195
```java
206196
Limit limitTen = Limit.of(10);
207197
```
@@ -213,14 +203,9 @@ Limit range = Limit.range(10,20);
213203

214204

215205
=== Paging
216-
When querying large amounts of data, paging is possible by adding a [hotspot=PageRequest file=0]`PageRequest` parameter to your query method:
206+
When querying large amounts of data, paging is possible by adding a [hotspot=PageRequest file=0]`PageRequest` parameter to your query method like in the [hotspot=PageRequest file=0]`all` method. This method will return all of the packages, but with the results paginated based on the provided [hotspot=PageRequest file=0]`PageRequest`.
217207

218-
```java
219-
@Find
220-
Page<Package> all(PageRequest pageRequest);
221-
```
222-
223-
A `PageRequest` can be constructed with the [hotspot=PageRequest file=0]`PageRequest` class's static methods. To request the first [hotspot=Page file=0]`Page` with a page size of 20 results:
208+
A [hotspot=PageRequest file=0]`PageRequest` can be constructed with the [hotspot=PageRequest file=0]`PageRequest` class's static methods. To request the first [hotspot=Page file=0]`Page` with a page size of 20 results:
224209

225210
```java
226211
Page<Package> page = packages.all(PageRequest.ofSize(20));
@@ -234,7 +219,7 @@ Page<Package> anotherPage = packages.all(page.nextPageRequest());
234219
```
235220

236221
== The Query Annotation
237-
The `@Query` annotation allows users to write queries using the Jakarta Data Query Language (JDQL). Complex queries can be written concisely using JDQL. JDQL is a strict subset of the Jakarta Persistence Query Language (JPQL).
222+
The `@Query` annotation allows users to write queries using the Jakarta Data Query Language (JDQL). Complex queries can be written concisely using JDQL. JDQL is a strict subset of the Jakarta Persistence Query Language (JPQL).
238223

239224
Packages.java
240225
[source, Java, linenums, role='code_column hide_tags=copyright']
@@ -248,26 +233,17 @@ include::finish/src/main/java/io/openliberty/guides/data/Packages.java[]
248233
`src/main/java/io/openliberty/guides/data/Packages.java`
249234
----
250235

251-
Here is an example of a complex query simplified using JDQL.
252-
253-
```java
254-
@Query("WHERE length > :threshold OR height > :threshold OR width > :threshold")
255-
List<Package> withDimensionLargerThan(float threshold);
256-
```
236+
For an example of a complex query simplified using JDQL, take a look at [hotspot=withAnyDimensionLargerThan file=0]`withAnyDimensionLargerThan`.
257237

258238
This query checks for packages where the length, height, or width are larger than a threshold, which is provided as a parameter in the function. The threshold parameter is referenced in the query using `:threshold`. Achieving the same query with Query by Method Name would result in a very long method name and additional parameters.
259239

260240

261-
JDQL supports named and ordinal parameters, as well as a number of operators.
262-
```java
263-
@Query("WHERE length + width + height > ?1")
264-
List<Package> withTotalDimensionOver(float threshold);
265-
```
241+
JDQL supports both named and positional parameters, as well as a number of operators. This is shown in the [hotspot=withTotalDimensionOver file=0]`withTotalDimensionOver` query, which sums the length, width, and height of each package to check if the total exceeds a provided threshold. Unlike the named parameter syntax (`:threshold`) used in the previous example, this query uses positional parameter syntax with `?1` to reference the first method parameter.
266242

267243
See the https://jakarta.ee/specifications/data/1.0/jakarta-data-1.0#_jakarta_data_query_language[Jakarta Data specification] for a more comprehensive overview of the Jakarta Data Query Language.
268244

269245
== Where to next?
270-
Experiment by creating your own queries. Use the existing ones in the `Packages` class as a starting point and then test them out in the sample application. For example, you could add a query to find packages arriving in a specific destination, which is ordered by width:
246+
Experiment by creating your own queries. Use the existing ones in the `Packages` class as a starting point and then test them out in the sample application. For example, you could add a query to find packages arriving at a specific destination, which is ordered by width:
271247

272248
```java
273249
@Find

finish/src/main/java/io/openliberty/guides/data/Packages.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,15 @@ public interface Packages extends CrudRepository<Package, Integer> {
9797
// end::sorting[]
9898
// tag::query-anno[]
9999

100+
// tag::withAnyDimensionLargerThan[]
100101
@Query("WHERE length > :threshold OR height > :threshold OR width > :threshold")
101-
List<Package> withDimensionLargerThan(float threshold);
102+
List<Package> withAnyDimensionLargerThan(float threshold);
103+
// end::withAnyDimensionLargerThan[]
102104

105+
// tag::withTotalDimensionOver[]
103106
@Query("WHERE length + width + height > ?1")
104107
List<Package> withTotalDimensionOver(float threshold);
108+
// end::withTotalDimensionOver[]
105109
// end::query-anno[]
106110

107111
}

0 commit comments

Comments
 (0)