0

I run in to a syntax problem where a union cannot be inside a union-all block when I attempt to use blaze persistence with queryDsl integration.

I am trying to union the result of two tables into one. Here is my original code:

blazeJPAQueryFactory
                .select(
                        QLikeRecordCte.likeRecordCte.id,
                        QLikeRecordCte.likeRecordCte.createTime,
                        QLikeRecordCte.likeRecordCte.userId,
                        QLikeRecordCte.likeRecordCte.actionId
                )
                .from(
                        JPQLNextExpressions.select(QLikeRecordCte.likeRecordCte)
                                .union(
                                        JPQLNextExpressions.select(QLikeRecordCte.likeRecordCte)
                                                .from(QTopicCommentLikeRecordEntity.topicCommentLikeRecordEntity)
                                                .bind(QLikeRecordCte.likeRecordCte.id, QTopicCommentLikeRecordEntity.topicCommentLikeRecordEntity.id)
                                                .bind(QLikeRecordCte.likeRecordCte.userId, QTopicCommentLikeRecordEntity.topicCommentLikeRecordEntity.userId)
                                                .bind(QLikeRecordCte.likeRecordCte.createTime, QTopicCommentLikeRecordEntity.topicCommentLikeRecordEntity.createTime)
                                                .bind(QLikeRecordCte.likeRecordCte.actionId, QTopicCommentLikeRecordEntity.topicCommentLikeRecordEntity.commentId),
                                        JPQLNextExpressions.select(QLikeRecordCte.likeRecordCte)
                                                .from(QTopicTitleLikeRecordEntity.topicTitleLikeRecordEntity)
                                                .bind(QLikeRecordCte.likeRecordCte.id, QTopicTitleLikeRecordEntity.topicTitleLikeRecordEntity.id)
                                                .bind(QLikeRecordCte.likeRecordCte.userId, QTopicTitleLikeRecordEntity.topicTitleLikeRecordEntity.userId)
                                                .bind(QLikeRecordCte.likeRecordCte.createTime, QTopicTitleLikeRecordEntity.topicTitleLikeRecordEntity.createTime)
                                                .bind(QLikeRecordCte.likeRecordCte.actionId, QTopicTitleLikeRecordEntity.topicTitleLikeRecordEntity.titleId)

                                )
                        ,
                        QLikeRecordCte.likeRecordCte
                ).fetch()

run this code and hibernate produces the following sql:

select
    likerecord0_.id as id1_0_,
    likerecord0_.action_id as action_i2_0_,
    likerecord0_.create_time as create_t3_0_,
    likerecord0_.user_id as user_id4_0_
from
    (
    select
        null id,
        null user_id,
        null create_time,
        null action_id
    from
        dual
    where
        1 = 0
union all (
    select
        topiccomme0_.id as col_0_0_,
        topiccomme0_.user_id as col_1_0_,
        topiccomme0_.create_time as col_2_0_,
        topiccomme0_.comment_id as col_3_0_
    from
        t_topic_comment_like_record topiccomme0_
    where
        ( topiccomme0_.del_flag = 0)
union
    select
        topictitle0_.id as col_0_0_,
        topictitle0_.user_id as col_1_0_,
        topictitle0_.create_time as col_2_0_,
        topictitle0_.title_id as col_3_0_
    from
        t_topic_title_like_record topictitle0_
    where
        ( topictitle0_.del_flag = 0))) likerecord0_

(you can ignore the del_flag = 0 part as it does not influence the result)

since there is a union inside the union-all block, I get a sql error heck the manual that corresponds to your MySQL server version for the right syntax to use near 'union

well the produced sql is at least conceptually clear and it would work if I just remove the whole 'select from dual and union all' part. I don't know why blaze have to wrap it with an union-all though.

Anyway I want the generated hibernate sql to be like this or at least close to the form of this:

select
    like_record.id,
    like_record.create_time,
    like_record.user_id,
    like_record.action_id
from (
    select
        t1.id as id,
        t1.user_id as user_id,
        t1.create_time as create_time,
        t1.comment_id as action_id
    from
        t_topic_comment_like_record t1
union
    select
        t2.id as id,
        t2.user_id as user_id,
        t2.create_time as create_time,
        t2.title_id as action_id
    from
        t_topic_title_like_record t2
) like_record

Please anyone tell me why the error occur and how to fix it. Any help is appreciated.

pterogum
  • 3
  • 1
  • 1
    to be more precise, the syntax error is related to the nested union in the union-all. It seems my version of Mysql does not support nested unions. If I simply remove the parenthesis and tweak the select part a bit, the sql will work functionally. – pterogum May 25 '23 at 06:58

0 Answers0