【问题标题】:create a where clause in Querydsl to examine a Timestamp column在 Querydsl 中创建 where 子句以检查 Timestamp 列
【发布时间】:2012-05-28 16:34:48
【问题描述】:

我正在尝试创建一个 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)

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

【问题讨论】:

    标签: querydsl


    【解决方案1】:

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

    不安全的做法是

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-06-24
      • 1970-01-01
      • 2013-05-31
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多