我正在尝试使用 jpa 2.0 进行子字符串查询,但我不知道该怎么做。 到目前为止我有这个代码:
time = builder.between(
matchesRoot.<Integer>get("startDateTime").toString().substring(8, 13),
Integer.getInteger(DateTimeUtil.dateTimeWithoutFormat(searchCommercialsDTO.getFinalDate())),
Integer.getInteger(DateTimeUtil.dateTimeWithoutFormat(searchCommercialsDTO.getIniDate()))
);
但它不起作用,因为由于此错误我无法编译它:
no suitable method found for between(String,Integer,Integer) method CriteriaBuilder.between(Expression,Y#1,Y#1) is not applicable (no instance(s) of type variable(s) Y#1 exist so that argument type String conforms to formal parameter type Expression) method CriteriaBuilder.between(Expression,Expression,Expression) is not applicable (no instance(s) of type variable(s) Y#2 exist so that argument type String conforms to formal parameter type Expression) where Y#1,Y#2 are type-variables: Y#1 extends Comparable declared in method between(Expression,Y#1,Y#1) Y#2 extends Comparable declared in method between(Expression,Expression,Expression
有什么建议吗?谢谢!
最佳答案
子字符串可以通过 CriteriaBuilder.substring 使用。当然,这不是对整数进行操作,而是对字符串进行操作。
在当前方法中,您正在调用 Path 的 toString 方法。结果 SQL 中的 substring 函数与用于构建查询的类的 toString 方法之间没有任何联系。
关于java - 如何在 criteriaQuery api (jpa 2.0) 中使用子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15616903/