user1422013
user1422013

Reputation: 31

create a where clause in Querydsl to examine a Timestamp column

I am trying to create a where clause which examines if a value stored in a column (which is identified in the "Q" class as

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

) is Greater than or equal to a supplied value.

The value is supplied as a long but (naturally) I can create any necessary data type before examining.

I have tried the following:

{
....
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 );
}

The code compiles and runs up to the point where the "where" clause is added. It then falls over with the following:

....
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)

I would be grateful for any thoughts on the matter.

Upvotes: 3

Views: 8429

Answers (1)

Timo Westk&#228;mper
Timo Westk&#228;mper

Reputation: 22190

You try to turn the constant 28.05.2012 into a Path. Paths are used for variables and properties. Another problem is that you want to compare a Timestamp to a String. In my opinion the best option would be to construct a Timestamp from your String constant and compare the path to it.

An unsafe thing to do would be

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

but this depends on how dates are serialized to Strings / varchars on the database level.

Also Querydsl queries are mutable, so no need to reassign them all the time.

Upvotes: 3

Related Questions