Spring Boot JPA Specification for Compare Dates

Viewed 6726

I need to find the table records which will come between the dates passed by the user. I am trying to write a specification for this but it is showing me compile time error as below :

The method between(Expression<? extends Y>, Expression<? extends Y>, Expression<? extends Y>) 
in the type CriteriaBuilder 
is not applicable for the arguments (Expression<Date>, Object, Object)

I have tried search on the various forums but didn't able to get how to solve this issue,may be I am doing something wrong. Please help me on this.

Specification Class

public class ScheduleClassSpecification implements Specification<ScheduleClassInformation> {

private SearchCriteria criteria;

@Override
public Predicate toPredicate(Root<ScheduleClassInformation> root,
        CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
    Expression<String> expression;
    Predicate predicate =null;

    if((criteria.getKey().equalsIgnoreCase("student"))){
         expression = root.join("course").join("student").get("student");
         predicate = criteriaBuilder.equal(expression, criteria.getValue());
    }else if(criteria.getKey().equalsIgnoreCase("startDate") || criteria.getKey().equalsIgnoreCase("endDate")){
        predicate = criteriaBuilder.between
                        (root.<Date>get(criteria.getKey()).as(java.util.Date.class), 
                                criteria.getValue(), 
                                criteria.getValue());  // Compile Time Error on this line

    }

    return predicate;
}

Criteria Class

public class SearchCriteria {
    private String key;
    private String operation;
    private Object value;}

Service For Extracting the record from Repository

if(!Utility.isNull(Id)){
                idSpec = new ScheduleClassSpecification(new SearchCriteria("Student",":",Id));
            }

            Page<SCOutput>  listreturn = scRepo.findAll(Specification.where(idSpec), SCOutput.class,new PageRequest(0, 100));
1 Answers
Related