如何从 QueryDSL 中的不同表进行联合

How to do unions from different tables in QueryDSL

我的问题很简单,如何使用 SQL QueryDSL 从不同的表中 select 例如

SELECT cat.name as name, cat.voice as voice , cat.purr FROM cat UNION
SELECT dog.name as name, dog.voice as voice , NULL as voice FROM dog;

像这样的东西应该可以工作

QCat cat = QCat.cat;
QDog dog = QDog.dog;

StringPath name = Expressions.stringPath("name");
StringPath voice = Expressions.stringPath("voice");
StringPath purr = Expressions.stringPath("purr");

SQLQueryFactory queryFactory = new SQLQueryFactory(MySQLTemplates.DEFAULT, null);
SQLQuery<Tuple> cats =
    SQLExpressions.select(cat.nome.as(name), cat.voice.as(voice), cat.purr.as(purr)).from(cat);
SQLQuery<Tuple> dogs =
    SQLExpressions.select(
            dog.nome.as(name),
            dog.voice.as(voice),
            Expressions.as(Expressions.nullExpression(), purr))
        .from(dog);
queryFactory.select(name, voice, purr).from(SQLExpressions.union(cats, dogs)).fetch();