2cadb54a3c0a436577c7ee97c5f1874fe48140b0
[JavaForFun] /
1 package de.spring.example.persistence.domain.specifications;
2
3 import java.time.LocalDate;
4
5 //import javax.persistence.criteria.CriteriaBuilder;
6 //import javax.persistence.criteria.CriteriaQuery;
7 //import javax.persistence.criteria.Predicate;
8 //import javax.persistence.criteria.Root;
9
10 import org.springframework.data.jpa.domain.Specification;
11
12 import de.spring.example.persistence.domain.Ad;
13
14 public class AdSpectifications {
15         
16 //      public static Specification<Ad> createdToday() {
17 //              return new Specification<Ad>() {
18 //
19 //                      @Override
20 //                      public Predicate toPredicate(Root<Ad> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
21 //                              final LocalDate date = LocalDate.now();
22 //                              
23 //                              return cb.equal(root.get("createdAt"), date);
24 //                      }
25 //
26 //              };
27 //              
28 //      }
29         
30         /**
31          * The same using lambda expressions
32          */
33         public static Specification<Ad> createdToday() {
34                 return (root, query, cb) -> {
35                         final LocalDate date = LocalDate.now();
36                         
37                         return cb.equal(root.get("createdAt"), date);
38         };
39         }
40         
41 //      public static Specification<Ad> mobileImage(String image) {
42 //              return new Specification<Ad>() {
43 //
44 //                      @Override
45 //                      public Predicate toPredicate(Root<Ad> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
46 //                              
47 //                              return cb.equal(root.get("adMobileImage"), image);
48 //                      }
49 //
50 //              };
51 //              
52 //      }
53         
54         /**
55          * The same using lambda expressions
56          */
57         public static Specification<Ad> mobileImage(String image) {
58                 return (root, query, cb) -> {
59                         return cb.equal(root.get("adMobileImage"), image);
60         };
61         }
62 }