You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 builtin Repositories with common methods which can be extended with applicationspecific 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.
98
98
99
99
[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.
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.
183
183
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:
190
185
```java
191
186
Sort sort = Sort.desc("height");
192
187
```
193
188
194
189
=== 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.
196
191
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.
203
193
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:
205
195
```java
206
196
Limit limitTen = Limit.of(10);
207
197
```
@@ -213,14 +203,9 @@ Limit range = Limit.range(10,20);
213
203
214
204
215
205
=== 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`.
217
207
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:
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).
For an example of a complex query simplified using JDQL, take a look at [hotspot=withAnyDimensionLargerThan file=0]`withAnyDimensionLargerThan`.
257
237
258
238
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.
259
239
260
240
261
-
JDQL supports named and ordinal parameters, as well as a number of operators.
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.
266
242
267
243
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.
268
244
269
245
== 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:
0 commit comments