How to build an OrderSpecifier from a Path

490 views
Skip to first unread message

Tullio Bettinazzi

unread,
May 4, 2016, 4:32:50 AM5/4/16
to Querydsl
I use querydsl-sql and don't use Q types.
I want to construct my query suing paths and I'm able to write everithing using path as shown below but I'm not hable to build tge order specifier using Paths.
If I try to substitute
OrderSpecifier os = prova.getNumber("PREV_ID",Long.class).asc();
with
OrderSpecifier os = ((ComparableExpressionBase<Boolean>)prova.get(prevId)).asc();
I get a ClassCastException.
How can I do it ?
Tks
Tullio

        PathBuilder<Tuple> prova = new PathBuilder<>(Tuple.class, "TB$PREVENTIVI");
        Path prevId = prova.get("PREV_ID");
        Path stato = prova.get("STATO");
        Param<Object> param1 = new Param<>(Object.class, "paramName");
        Predicate filter = prova.get(stato).eq(param1);
        OrderSpecifier os = prova.getNumber("PREV_ID",Long.class).asc();
        SQLQueryFactory factory = new SQLQueryFactory(sqlConfiguration, pc);    
        SQLQuery query = factory.query().from(prova).where(filter).orderBy(os);
        query.set(param1,"IN ESECUZIONE DA SOSPENSIONE");
        List<Tuple> lista = query.list(prevId,stato);

Ruben Dijkstra

unread,
May 4, 2016, 6:59:19 AM5/4/16
to Querydsl
There is a getComparable() method, that way you don't need to cast.

Best regards,

Ruben

Tullio Bettinazzi

unread,
May 4, 2016, 10:50:29 AM5/4/16
to Querydsl
The object I receive from
Path prevId = prova.get("PREV_ID");
is a PathBuilder and doesn't expose any getComparableMethod().
Where shoud I put it ?
Tks

Ruben Dijkstra

unread,
May 4, 2016, 11:02:53 AM5/4/16
to Querydsl
PathBuilder has a couple of get*() methods, that all have a specific type.


For you, that would be getComparable().

Best regards,

Ruben Dijkstra

Tullio Bettinazzi

unread,
May 4, 2016, 11:19:31 AM5/4/16
to Querydsl
The .getComparable method requires again the name of the field even if in
PathBuilder prevId = prova.get("PREV_ID");
is already defined the name of the field.
Is it normal ?
Tks

Ruben Dijkstra

unread,
May 4, 2016, 11:36:18 AM5/4/16
to Querydsl
Could you clarify?

I mean, when you take this
OrderSpecifier os = ((ComparableExpressionBase<Boolean>)prova.get(prevId)).asc();

And turn it into this
OrderSpecifier os = prova.getComparable(prevId, Comparable.class).asc();

You get the right type.

Best regards,

Ruben Dijkstra

Tullio Bettinazzi

unread,
May 4, 2016, 11:41:03 AM5/4/16
to Querydsl
Tryed but I got the following compilation error :
The method getComparable(String, Class<A>) in the type PathBuilder<Tuple> is not applicable for the arguments (PathBuilder, Class<Comparable>)
Tks

Ruben Dijkstra

unread,
May 4, 2016, 12:35:51 PM5/4/16
to Querydsl
Oh I see,
I meant
OrderSpecifier os = prova.getComparable("PREV_ID", Comparable.class).asc();

That is the column name right?

The PathBuilders can be used for chaining nested properties (user.relation...), and getComparable, getString etc. are for the properties themselves.
Reply all
Reply to author
Forward
0 new messages