java - 如何在 criteriaQuery api (jpa 2.0) 中使用子字符串

标签 java jpa-2.0 criteria-api

我正在尝试使用 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/

相关文章:

java - 如何从PDF文件中提取没有页眉和页脚的文本内容

java - 带选择的 CriteriaBuilder 不允许没有构造函数

java - 如何在 Java 中发出 http 部分 GET 请求

java - 在 Heroku 上运行 Web 应用程序

java - 如何使用 StAX 添加架构位置

java - JPA 与 JTA : Persist entity and merge cascaded child entities

jpa-2.0 - 使用 JPA 加载懒惰 vs 急切加载

java - JPA 的类型转换取决于同一实体中的另一个属性

java - 如何: Hibernate 3. xx条件查询

hibernate - 连接字段上的 JPA 查询过滤器