java 使用hibernate和SQL Server使用distinct子句进行分页

vq8itlhq  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(92)

我使用JPA和Spring Data在SQL Server上创建了一个可分页的查询,使用这个存储库调用:

@Query("select distinct nat from TABE1 nat " +
            "left join TABLE2 natr on natr.ID= nat.ID" +
            "left join TABLE3 rec on rec.ID= natr.ID_" +
            "left join TABLE4 ew on ew.ID= rec.ID_ " +
            "where (ew.status = 'VA' or nat.isM = true) " +
            "and nat.expDate = :endTime " +
            "and nat.dDate = :endTime"
    )
    Page<Entity1> findAllActiveH(@Param("endTime") LocalDateTime endOfTime,
                                                     Pageable pageable);

接下来是用Hibernate生成的查询

select
        distinct t.id as id1_47_,
        ...ALL column
    from
        TABLE t 
    left outer join
     ..... ALL Joint 
    where
       Conditions...
    order by 1,
        case 
            when t.creation_date is null then 1 
            else 0 
        end,
        t.creation_date desc,
        case 
            when lower(t.id) is null then 1 
            else 0 
        end,
        lower(t.id) asc offset 0 rows fetch next 10 rows only

生成的查询附加了几个关于可分页对象的“order by”,但引发了以下异常
如果指定了SELECT DISTINCT,则ORDER BY项必须出现在选择列表中
我不能删除不同的,以避免数据重复任何想法如何处理分页和不同的Spring数据/休眠和SQL Server?
谢谢

628mspwn

628mspwn1#

{ @Query("select distinct nat from TABE1 nat " +
            "left join TABLE2 natr on natr.ID= nat.ID" +
            "left join TABLE3 rec on rec.ID= natr.ID_" +
            "left join TABLE4 ew on ew.ID= rec.ID_ " +
            "where (ew.status = 'VA' or nat.isM = true) " +
            "and nat.expDate = :endTime " +
            "and nat.dDate = :endTime ORDER BY nat" }

你需要提及现场订货

相关问题