querydsl - 在 Querydsl 中创建一个 where 子句以检查时间戳列

标签 querydsl

我正在尝试创建一个 where 子句,它检查一个值是否存储在列中(在“Q”类中标识为

    public final DateTimePath<java.sql.Timestamp> startDate = createDateTime("StartDate",    java.sql.Timestamp.class);

) 大于或等于提供的值。

值以 long 形式提供,但(自然地)我可以在检查之前创建任何必要的数据类型。

我尝试了以下方法:

{
....
final QViewVETFullList fullList = QViewVETFullList.viewVETFullList;

....
final String startDate = "28.05.2012"; // test data
final BooleanExpression startDateExp = getDateGTEExpression(fullList.startDate, startDate);

.....
query = query.from(fullList);
query = query.where(startDateExp); // this is where it falls over

...

}

    public static BooleanExpression getDateGTEExpression(
        DateTimePath<Timestamp> path, String dateStr) {

            // this seems to be the problem??
    final DateTimePath<Timestamp> datePath = Expressions.dateTimePath(
            java.sql.Timestamp.class, dateStr);

            // this syntax works successfully with other data types
    return BooleanOperation.create(Ops.GOE, path, datePath );
}

代码编译并运行到添加“where”子句的位置。 然后它以以下内容结束:

....
Caused by: java.lang.IllegalArgumentException: Undeclared path 28.05.2012
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:48)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:10)
at com.mysema.query.types.path.DateTimePath.accept(DateTimePath.java:46)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:101)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:36)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:10)
at com.mysema.query.types.expr.BooleanOperation.accept(BooleanOperation.java:44)
at com.mysema.query.DefaultQueryMetadata.validate(DefaultQueryMetadata.java:296)
at com.mysema.query.DefaultQueryMetadata.addWhere(DefaultQueryMetadata.java:138)
at com.mysema.query.support.QueryMixin.where(QueryMixin.java:375)
at com.mysema.query.support.QueryBase.where(QueryBase.java:44)
at com.***.***.***.***.***DAOBean.get*****(***tDAOBean.java:185)

如果您对此事有任何想法,我将不胜感激。

最佳答案

您尝试将常量 2​​8.05.2012 转换为路径。路径用于变量和属性。另一个问题是您想将时间戳与字符串进行比较。在我看来,最好的选择是从您的字符串常量构造一个时间戳并将路径与它进行比较。

不安全的事情是

query.from(fullList)
     .where(fullList.startDate.stringValue().goe("28.05.2012"))

但这取决于日期在数据库级别如何序列化为 Strings/varchars。

此外,Querydsl 查询是可变的,因此无需一直重新分配它们。

关于querydsl - 在 Querydsl 中创建一个 where 子句以检查时间戳列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10787838/

相关文章:

java - 具有偏移和限制的 0..n 结果转换会产生不准确的结果

mongodb - QueryDsl MongoRepository 投影

elasticsearch - 术语字段汇总

java - Querydsl maven编译错误: QClass.类不存在

java - QueryDSL 多参数搜索,同时在值为 null 时构建查询跳过谓词

java - QueryDSL 在构建谓词查询时添加交叉连接

jpa - QueryDSL 中的平均日期差异

java - 如何使用Spring-Data和QueryDSL?

spring-data - QueryDSL - 添加子查询到 FROM 语句

java - JPQL `index()` 函数的 QueryDSL 等效项是什么?