From 0efbb12e2b1758c30e9767513df8163d0d47e25e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 6 Feb 2013 18:36:37 +0100 Subject: [PATCH] DATAJPA-291 - Polished Specification documentation. Explained usage of JPA metamodel. Fixed errors in sample code (method visibility and generics). --- src/docbkx/jpa.xml | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/docbkx/jpa.xml b/src/docbkx/jpa.xml index f389067c9..293021e3d 100644 --- a/src/docbkx/jpa.xml +++ b/src/docbkx/jpa.xml @@ -1,7 +1,7 @@ - + JPA Repositories @@ -668,7 +668,7 @@ int setFixedFirstnameFor(String firstname, String lastname); public static Specification<Customer> isLongTermCustomer() { return new Specification<Customer>() { - Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, + public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder builder) { LocalDate date = new LocalDate().minusYears(2); @@ -679,7 +679,7 @@ int setFixedFirstnameFor(String firstname, String lastname); public static Specification<Customer> hasSalesOfMoreThan(MontaryAmount value) { return new Specification<Customer>() { - Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, + public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { // build query here @@ -691,8 +691,15 @@ int setFixedFirstnameFor(String firstname, String lastname); Admittedly the amount of boilerplate leaves room for improvement (that will hopefully be reduced by Java 8 closures) but the client side - becomes much nicer as you will see below. Besides that we have expressed - some criteria on a business requirement abstraction level and created + becomes much nicer as you will see below. The + Customer_ type is a metamodel type generated using + the JPA Metamodel generator (see the Hibernate + implementation's documentation for example). So the expression + Customer_.createdAt is asuming the + Customer having a createdAt attribute + of type Date. Besides that we have expressed some + criteria on a business requirement abstraction level and created executable Specifications. So a client might use a Specification as follows: @@ -885,11 +892,11 @@ public interface UserRepository extends JpaRepository<User, Long> {
- Auditing - - - + + +