Skip to content

Commit

Permalink
use @query and @find for example code in chapter 4
Browse files Browse the repository at this point in the history
instead of QbMN
  • Loading branch information
gavinking committed Apr 5, 2024
1 parent 37aa728 commit a4938c2
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions spec/src/main/asciidoc/repository.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,10 @@ The following example demonstrates the use of special parameters:
@Repository
public interface ProductRepository extends BasicRepository<Product, Long> {
@Find
Page<Product> findByName(String name, PageRequest pageRequest, Order<Product> order);
@Query("where name like :pattern")
List<Product> findByNameLike(String pattern, Limit max, Sort<?>... sorts);
}
Expand Down Expand Up @@ -353,26 +355,32 @@ The following examples work through scenarios where static and dynamic sort crit
[source,java]
----
// Sorts first by type. When type is the same, applies the Order's sort criteria
Page<User> findByNameStartsWithOrderByType(String namePrefix,
PageRequest pagination,
Order<User> sorts);
@Query("where left(name,length(?1)) = ?1")
@OrderBy(_User.TYPE)
Page<User> findByNamePrefix(String namePrefix,
PageRequest pagination,
Order<User> sorts);
// Sorts first by type. When type is the same, applies the criteria in the Sorts
List<User> findByNameStartsWithOrderByType(String namePrefix, Sort<?>... sorts);
@Query("where left(name,length(?1)) = ?1")
@OrderBy(_User.TYPE)
List<User> findByNamePrefix(String namePrefix, Sort<?>... sorts);
// Sorts first by age. When age is the same, applies the Order's sort criteria
@OrderBy("age")
Page<User> findByNameStartsWith(String namePrefix,
PageRequest pagination,
Order<User> sorts);
@Query("where left(name,length(?1)) = ?1")
@OrderBy(_User.AGE)
Page<User> findByNamePrefix(String namePrefix,
PageRequest pagination,
Order<User> sorts);
// Sorts first by age. When age is the same, applies the criteria in the Sorts
@OrderBy("age")
List<User> findByNameStartsWith(String namePrefix, Sort<?>... sorts);
@Query("where left(name,length(?1)) = ?1")
@OrderBy(_User.AGE)
List<User> findByNamePrefix(String namePrefix, Sort<?>... sorts);
// Sorts first by name. When name is the same, applies the Order's sort criteria
@Query("WHERE (u.age > ?1)")
@OrderBy("name")
@Query("WHERE u.age > ?1")
@OrderBy(_User.NAME)
CursoredPage<User> olderThan(int age, PageRequest pagination, Order<User> sorts);
----

Expand Down Expand Up @@ -520,8 +528,11 @@ For example,
----
@Repository
public interface CustomerRepository extends BasicRepository<Customer, Long> {
CursoredPage<Customer> findByZipcodeOrderByLastNameAscFirstNameAscIdAsc(
int zipcode, PageRequest pageRequest);
@Find
@OrderBy("lastName")
@OrderBy("firstName")
@OrderBy("id")
CursoredPage<Customer> findByZipcode(int zipcode, PageRequest pageRequest);
}
----

Expand All @@ -531,10 +542,10 @@ You can obtain the initial page relative to an offset and subsequent pages relat
----
PageRequest pageRequest = PageRequest.ofSize(50);
Page<Customer> page =
customers.findByZipcodeOrderByLastNameAscFirstNameAscIdAsc(55901, pageRequest);
customers.findByZipcode(55901, pageRequest);
if (page.hasNext()) {
pageRequest = page.nextPageRequest();
page = customers.findByZipcodeOrderByLastNameAscFirstNameAscIdAsc(55901, pageRequest);
page = customers.findByZipcode(55901, pageRequest);
...
}
----
Expand All @@ -546,24 +557,25 @@ Or you can obtain the next (or previous) page relative to a known entity,
Customer c = ...
PageRequest p = PageRequest.ofSize(50)
.afterKey(c.lastName, c.firstName, c.id);
page = customers.findByZipcodeOrderByLastNameAscFirstNameAscIdAsc(55902, p);
page = customers.findByZipcode(55902, p);
----

The sort criteria for a repository method that performs cursor-based pagination must uniquely identify each entity and must be provided by:

* `OrderBy` name pattern of the repository method (as in the examples above) or `@OrderBy` annotation(s) on the repository method.
* `Order` and `Sort` parameters of the repository method.
* the `@OrderBy` annotation or annotations of the repository method,
* `Order` or `Sort` parameters of the repository method, or
* an `OrderBy` in Query by Method Name.

The values of the entity attributes of the combined sort criteria define the cursor for cursor-based cursor based pagination. Within the cursor, each entity attribute has the same sorting and order of precedence that it has within the combined sort criteria.

===== Example of Appending to Queries for Cursor-based Pagination

Without cursor-based pagination, a Jakarta Data provider that is based on Jakarta Persistence might compose the following JPQL for the `findByZipcodeOrderByLastNameAscFirstNameAscIdAsc` repository method from the prior example:
Without cursor-based pagination, a Jakarta Data provider that is based on Jakarta Persistence might compose the following JPQL for the `findByZipcode()` repository method from the prior example:

[source,jpaql]
----
FROM Customer
WHERE (zipCode = ?1)
WHERE zipCode = ?1
ORDER BY lastName ASC, firstName ASC, id ASC
----

Expand Down Expand Up @@ -641,9 +653,10 @@ In this example, the application uses a cursor to request pages in forward and p
----
@Repository
public interface Products extends CrudRepository<Product, Long> {
CursoredPage<Product> findByNameLike(String namePattern,
PageRequest pageRequest,
Order<Product> sorts);
@Query("where name like ?1")
CursoredPage<Product> findByName(String namePattern,
PageRequest pageRequest,
Order<Product> sorts);
}
----

Expand All @@ -657,7 +670,7 @@ Order<Product> order = Order.by(_Product.price.asc(),
PageRequest pageRequest = PageRequest.ofSize(10)
.afterKey(priceMidpoint, 0L);
CursoredPage<Product> moreExpensive =
products.findByNameLike(pattern, pageRequest, order);
products.findByName(pattern, pageRequest, order);
----

Obtaining the previous 10 products:
Expand All @@ -668,7 +681,7 @@ pageRequest = moreExpensive.hasContent()
? pageRequest.beforeCursor(moreExpensive.getCursor(0))
: pageRequest.beforeKey(priceMidpoint, 1L);
CursoredPage<Product> lessExpensive =
products.findByNameLike(pattern, pageRequest, order);
products.findByName(pattern, pageRequest, order);
----

===== Example with Combined Sort Criteria
Expand All @@ -680,7 +693,7 @@ In this example, the application uses `OrderBy` to define a subset of the sort c
@Repository
public interface Products extends CrudRepository<Product, Long> {
@Find
@OrderBy(_Car.vehicleCondition)
@OrderBy(_Car.VEHICLE_CONDITION)
CursoredPage<Car> find(@By(_Car.make) String manufacturer,
@By(_Car.model) String model,
PageRequest pageRequest,
Expand Down

0 comments on commit a4938c2

Please sign in to comment.