DATAJPA-1787 - Updates Specifications documentation.
Updates Specifications documentation to use java 8 lambdas instead of anonymous classes. This improves readability and reduces boilerplate code. Original pull request: #429.
This commit is contained in:
committed by
Jens Schauder
parent
2b014ecb76
commit
7da43738bf
@@ -809,31 +809,23 @@ Specifications can easily be used to build an extensible set of predicates on to
|
||||
----
|
||||
public class CustomerSpecs {
|
||||
|
||||
public static Specification<Customer> isLongTermCustomer() {
|
||||
return new Specification<Customer>() {
|
||||
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query,
|
||||
CriteriaBuilder builder) {
|
||||
|
||||
LocalDate date = new LocalDate().minusYears(2);
|
||||
return builder.lessThan(root.get(Customer_.createdAt), date);
|
||||
}
|
||||
public static Specification<Customer> isLongTermCustomer() {
|
||||
return (root, query, builder) -> {
|
||||
LocalDate date = LocalDate.now().minusYears(2);
|
||||
return builder.lessThan(root.get(Customer_.createdAt), date);
|
||||
};
|
||||
}
|
||||
|
||||
public static Specification<Customer> hasSalesOfMoreThan(MonetaryAmount value) {
|
||||
return new Specification<Customer>() {
|
||||
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
|
||||
CriteriaBuilder builder) {
|
||||
|
||||
// build query here
|
||||
}
|
||||
return (root, query, builder) -> {
|
||||
// build query here
|
||||
};
|
||||
}
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
Admittedly, the amount of boilerplate leaves room for improvement (that may eventually be reduced by Java 8 closures), but the client side becomes much nicer, as you will see later in this section.
|
||||
The `Customer_` type is a metamodel type generated using the JPA Metamodel generator (see the link:$$https://docs.jboss.org/hibernate/jpamodelgen/1.0/reference/en-US/html_single/#whatisit$$[Hibernate implementation's documentation for an example]).
|
||||
So the expression, `Customer_.createdAt`, assumes the `Customer` has a `createdAt` attribute of type `Date`.
|
||||
Besides that, we have expressed some criteria on a business requirement abstraction level and created executable `Specifications`.
|
||||
|
||||
Reference in New Issue
Block a user