Search code examples
javaspringhibernatejpa

How to give Greater than Date value without passing parameter in JPA Date method


I am trying to fetch records which are greater than actionDate column using Spring JPA. I am able to achieve it if i write the method like below .

findAllByActionDateGreaterThan(Date date)

But Is there any way i can do it without passing the date parameter like a Hardcoded value in the method name itself like

findAllByActionDateGreaterThanSysdateMinus30()

Solution

  • Maybe it is a complicate workaround - but have you tried to use Java Specifications?

    public class TestSpec implements Specification<YourEntity> {
    @Override
    public Predicate toPredicate(Root<YourEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        Predicate yourPredicate
                = cb.and(cb.greaterThanOrEqualTo(root.get(dateInEntity), 
        return cb.and(yourPredicate);
    }
    

    then it should work like this: yourRepo.findAll(yourPredicate);

    here is more about that: https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl