Hibernate 6/Sping Boot 3 -为可分页的本地查询生成的SQL错误

v1l68za4  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(118)

我正在将一个项目迁移到Sping Boot 3(Hibernate 6)。下面的本机查询是可分页的(查询简化以重现问题):

`    @Query(
            value = """
                    select s.id as id, s.name as name, gp.points
                    from specialist s
                    left join (select q.specialist_id, listagg(q.points, ';') as points from qualification q group by q.specialist_id) gp on gp.specialist_id = s.id
                    where name like :name
                    """
            , nativeQuery = true)
    Page<SpecialistOverview> overview(@Param("name") String name, Pageable pageable);`

Hibernate生成的SQL如下所示:

select s.id as id, s.name as name, gp.points
from specialist s
         left join (select q.specialist_id, listagg(q.points, ' fetch first ? rows only;') as points
                    from qualification q
                    group by q.specialist_id) gp on gp.specialist_id = s.id
where name like ?
order by name asc

正如你所看到的,“fetch first?rows only”部分现在是listagg函数的分隔符文字的一部分。当执行此查询时,它将导致DataIntegrityViolationException抱怨参数不匹配(第二个?是文字的一部分)。
Sping Boot 3.0.4(Hibernate 6.1.7.Final)
使用Sping Boot 2.7.9(Hibernate 5.6.15.Final),一切都很好:

select s.id as id, s.name as name, gp.points
from specialist s
         left join (select q.specialist_id, listagg(q.points, ';') as points
                    from qualification q
                    group by q.specialist_id) gp on gp.specialist_id = s.id
where name like ?
order by name asc
limit ?

在打开Hibernate问题之前,我会在这里询问。我希望工作生成的SQL代码(如在Hibernate 5.6.x下)。

相关问题