Spring JPA: Specifications, using lambda expressions
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 12 Jul 2016 01:09:12 +0000 (03:09 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 12 Jul 2016 01:09:12 +0000 (03:09 +0200)
SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/specifications/AdSpectifications.java

index 6ee3644..2cadb54 100644 (file)
@@ -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<Ad> createdToday() {
+//             return new Specification<Ad>() {
+//
+//                     @Override
+//                     public Predicate toPredicate(Root<Ad> 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<Ad> createdToday() {
-               return new Specification<Ad>() {
-
-                       @Override
-                       public Predicate toPredicate(Root<Ad> 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<Ad> mobileImage(String image) {
+//             return new Specification<Ad>() {
+//
+//                     @Override
+//                     public Predicate toPredicate(Root<Ad> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
+//                             
+//                             return cb.equal(root.get("adMobileImage"), image);
+//                     }
+//
+//             };
+//             
+//     }
+       
+       /**
+        * The same using lambda expressions
+        */
        public static Specification<Ad> mobileImage(String image) {
-               return new Specification<Ad>() {
-
-                       @Override
-                       public Predicate toPredicate(Root<Ad> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
-                               
-                               return cb.equal(root.get("adMobileImage"), image);
-                       }
-
-               };
-               
+               return (root, query, cb) -> {
+                       return cb.equal(root.get("adMobileImage"), image);
+        };
        }
 }