Skip to content

Commit 2109632

Browse files
Eulodosschauder
authored andcommitted
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.
1 parent 13135d1 commit 2109632

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

src/main/asciidoc/jpa.adoc

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -809,31 +809,23 @@ Specifications can easily be used to build an extensible set of predicates on to
809809
----
810810
public class CustomerSpecs {
811811
812-
public static Specification<Customer> isLongTermCustomer() {
813-
return new Specification<Customer>() {
814-
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query,
815-
CriteriaBuilder builder) {
816812
817-
LocalDate date = new LocalDate().minusYears(2);
818-
return builder.lessThan(root.get(Customer_.createdAt), date);
819-
}
813+
public static Specification<Customer> isLongTermCustomer() {
814+
return (root, query, builder) -> {
815+
LocalDate date = LocalDate.now().minusYears(2);
816+
return builder.lessThan(root.get(Customer_.createdAt), date);
820817
};
821818
}
822819
823820
public static Specification<Customer> hasSalesOfMoreThan(MonetaryAmount value) {
824-
return new Specification<Customer>() {
825-
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
826-
CriteriaBuilder builder) {
827-
828-
// build query here
829-
}
821+
return (root, query, builder) -> {
822+
// build query here
830823
};
831824
}
832825
}
833826
----
834827
====
835828

836-
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.
837829
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]).
838830
So the expression, `Customer_.createdAt`, assumes the `Customer` has a `createdAt` attribute of type `Date`.
839831
Besides that, we have expressed some criteria on a business requirement abstraction level and created executable `Specifications`.

0 commit comments

Comments
 (0)