Spring Boot JPA规范的比较日期

时间:2018-11-05 08:48:39

标签: spring-boot jpa spring-data-jpa predicate criteria-api

我需要找到将在用户传递的日期之间的表记录。我正在尝试为此编写规范,但它向我显示了编译时错误,如下所示:

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)

我尝试在各种论坛上进行搜索,但无法获取解决此问题的方法,可能是我做错了。请帮助我。

规范类

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;
}

条件类

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

用于从存储库提取记录的服务

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 个答案:

答案 0 :(得分:1)

由于编译器错误提示您需要Expression

您可以轻松地做到这一点,只需将criteriaBuilder.literal(criteria.getValue())替换为criteria.getValue()