From 19092372178bc55f782f54b07e131b1bdf52d353 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 12 Jul 2016 03:09:12 +0200 Subject: [PATCH] Spring JPA: Specifications, using lambda expressions --- .../domain/specifications/AdSpectifications.java | 70 ++++++++++++++-------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/specifications/AdSpectifications.java b/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/specifications/AdSpectifications.java index 6ee3644..2cadb54 100644 --- a/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/specifications/AdSpectifications.java +++ b/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/specifications/AdSpectifications.java @@ -2,10 +2,10 @@ package de.spring.example.persistence.domain.specifications; import java.time.LocalDate; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; +//import javax.persistence.criteria.CriteriaBuilder; +//import javax.persistence.criteria.CriteriaQuery; +//import javax.persistence.criteria.Predicate; +//import javax.persistence.criteria.Root; import org.springframework.data.jpa.domain.Specification; @@ -13,30 +13,50 @@ import de.spring.example.persistence.domain.Ad; public class AdSpectifications { +// public static Specification createdToday() { +// return new Specification() { +// +// @Override +// public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { +// final LocalDate date = LocalDate.now(); +// +// return cb.equal(root.get("createdAt"), date); +// } +// +// }; +// +// } + + /** + * The same using lambda expressions + */ public static Specification createdToday() { - return new Specification() { - - @Override - public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { - final LocalDate date = LocalDate.now(); - - return cb.equal(root.get("createdAt"), date); - } - - }; - + return (root, query, cb) -> { + final LocalDate date = LocalDate.now(); + + return cb.equal(root.get("createdAt"), date); + }; } +// public static Specification mobileImage(String image) { +// return new Specification() { +// +// @Override +// public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { +// +// return cb.equal(root.get("adMobileImage"), image); +// } +// +// }; +// +// } + + /** + * The same using lambda expressions + */ public static Specification mobileImage(String image) { - return new Specification() { - - @Override - public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { - - return cb.equal(root.get("adMobileImage"), image); - } - - }; - + return (root, query, cb) -> { + return cb.equal(root.get("adMobileImage"), image); + }; } } -- 2.1.4