Create an alias on a function in querydsl

2020-07-17 16:07发布

问题:

How can I create an alias of a function in querydsl, so I can use it in an ORDER BY or GROUP BY statement, or in a subquery?

I tried this code :

StringPath idNiv0s = Expressions.stringPath("idNiv0s");

StringExpression func = Expressions.stringTemplate("replace({0},',','')", 
        Expressions.stringTemplate("substring_index( {0}, ',' , -2 )", ach.ancestors) );

new JPAQuery(entityManager).from(ach)
            .groupBy(idNiv0s)
            .orderBy(idNiv0s.desc())
            .list(new QCritereItem(  func.as(idNiv0s) , idNiv0s.count() ));

I got this exception

java.lang.IllegalArgumentException: Undeclared path 'idNiv0s'. 
              Add this path as a source to the query to be able to reference it.