From: Gustavo Martin Morcuende Date: Tue, 12 Jul 2016 01:09:12 +0000 (+0200) Subject: Spring JPA: Specifications, using lambda expressions X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=19092372178bc55f782f54b07e131b1bdf52d353;p=JavaForFun Spring JPA: Specifications, using lambda expressions --- 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); + }; } }