How do we select spring repositories with different parameters from base?



  • I have a search filter - a user name and two dates of start and end. How can I make a spleen on these fields using repository?

    @Service
    public class ResolutionServiceDefault implements ResolutionService {
        @Autowired
        private ResolutionRepository resolutionRepository;
    
    @Override
    public List<Resolution> findAllFilter(String user, Date start, Date end) {
        if(user!=null)...
        if(start!=null)...
        if(end!=null)...
        //выполнить запрос по полям которые не  null
        return .....;
    }
    

    }

    @Repository
    public interface ResolutionRepository extends JpaRepository<Resolution, Long> {
    List<Resolution> findAllByStatus(int status);
    List<Resolution> findAll();//String user, Date start, Date end
    }



  • Such tasks were addressed through the interface. Specification:

    public class ResolutionSpecs {
        public static Specification<Resolution> isActualUser(String user) {
            return new Specification<Resolution>() {
                public Predicate toPredicate(Root<Resolution> r, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                    return cb.equal(r.get(_Resolution.user), user);
                }
            };
        }
        public static Specification<Resolution> isSomeDateAfter(Date start) {
            return new Specification<Resolution>() {
                public Predicate toPredicate(Root<Resolution> r, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                    return cb.greater(r.get(_Resolution.someDate), start);
                }
            };
        }
        public static Specification<Resolution> isSomeDateBefore(Date end) {
            return new Specification<Resolution>() {
                public Predicate toPredicate(Root<Resolution> r, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                    return cb.lower(r.get(_Resolution.someDate), end);
                }
            };
        }
    }
    

    @Repository
    public interface ResolutionRepository extends JpaRepository<Resolution, Long>, JpaSpecificationExecutor<Resolution> {

    }

    @Service
    public class ResolutionService {
    private final ResolutionRepository repository;

    @Autowired
    public ResolutionService(ResolutionRepository repository) {
        this.repository = repository;
    }
    
    public List&lt;Resolution&gt; findByUserAndSomeDateBetween(String user, Date start, Date end) {
        Specification&lt;Resolution&gt; sp = null;
        if (user != null) {
            sp = Specifications.where(ResolutionSpecs.isActualUser(user));
        }
        if (start != null) {
            sp = (sp == null ? Specifications.where(ResolutionSpecs.isSomeDateAfter(start)) : sp.and(ResolutionSpecs.isSomeDateAfter(start)));
        }
        if (end != null) {
            sp = (sp == null ? Specifications.where(ResolutionSpecs.isSomeDateBefore(end)) : sp.and(ResolutionSpecs.isSomeDateBefore(end)));
        }
        return repository.findAll(sp);
    }
    

    }

    http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#specifications ♪

    There may also be a way to use a library to generate SQL queries, such as JOOQ, but I have no experience with it. http://www.jooq.org/doc/3.4/manual-single-page/#select-statement ♪ http://www.baeldung.com/jooq-with-spring ♪




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2