QueryDSL按顺序合并

时间:2018-06-04 15:15:49

标签: jpa jpql querydsl

我正在尝试连接两个表并输出它们并按字母顺序按order by coalesce(tableA.name, tableB.name)(非order by tableA.name, tableB.name)这两个字段对它们进行排序,因此结果应该是这样的:

tableA.name    tableB.name
A              null
B              null
null           C
D              null
null           E

在纯SQL中它工作正常,但是当我尝试使用QueryDSL时,它会向生成的select语句添加额外的列,并仅按第一个指定的列进行排序:

//java code
query.orderBy(qTableA.name.coalesce(qTableB.name).asc());

//generated sql code
SELECT ...
       COALESCE(tablea_.NAME, tableb_.NAME) AS col_9_0_
FROM   ...
WHERE  ...
ORDER  BY tablea1_.NAME ASC  

有人能说出为什么会这样做,是否有可能让它像我期望的那样工作?

1 个答案:

答案 0 :(得分:0)

尝试一下:

final Coalesce<String> coalesce =
    new Coalesce<>(String.class).add(optionalA).add(optionalB);

在选择字段中使用合并,因此在order by子句中使用

.orderBy(coalesce.asc()) // or desc()
相关问题