gpt4 book ai didi

java - Spring jpa native 查询排序添加前缀以按字段名称排序

转载 作者:行者123 更新时间:2023-12-04 13:02:58 24 4
gpt4 key购买 nike

我有排序问题。
存储方法:

@Query(nativeQuery = true,
value = "SELECT D.ID as dealerId , D.NAME as dealerName, K.ID as kpiId, " +
"K.NAME as kpiName FROM REGION R, DEALER D, KPI K "
+ "WHERE R.IMPORTER_ID = :importerId "
+ "AND D.REGION_ID = R.ID "
+ "AND K.IMPORTER_ID = :importerId ")

Page<DealersKpiTargets> getDealersKpiTargets(@Param("importerId") Long importerId, Pageable pageable);
可分页对象:
Page request [number: 0, size 20, sort: name: DESC]
hibernate 日志:
Hibernate: SELECT D.ID as dealerId , D.NAME as dealerName, K.ID as kpiId, K.NAME as kpiName FROM REGION R, DEALER D, KPI K WHERE R.IMPORTER_ID = ? AND D.REGION_ID = R.ID AND K.IMPORTER_ID = ?  order by R.name desc limit ?
我不明白在哪里 R.name前缀来自于 order by参加 Hibernate (接近尾声)。
作为引用,我正在使用:

spring-data-jpa version 2.0.7.RELEASE

spring-boot-starter-data-jpa version 2.0.2.RELEASE


更新
我通过将查询从 native 查询更改为 jpa 查询解决了这个问题,并且它正在工作。我改变了笛卡尔加入版本。
        @Query("SELECT dealerEntity.id AS dealerId , dealerEntity.name AS dealerName, kpiEntity.id AS kpiId, " +
"kpiEntity.name AS kpiName FROM KpiEntity kpiEntity "
+ "JOIN RegionEntity regionEntity ON regionEntity.importerEntity = kpiEntity.importerEntity "
+ "JOIN DealerEntity dealerEntity ON dealerEntity.importerEntity = regionEntity.importerEntity "
+ "WHERE kpiEntity.importerEntity = :importerEntity ")
Page<DealersKpiTargets> getDealersKpiTargets(@Param("importerEntity") ImporterEntity importerEntity, Pageable pageable);

最佳答案

这是 jira 票,其中包含更多详细信息,这可能是解决问题的关键( https://jira.spring.io/browse/DATAJPA-1613 )。

QueryUtils.ALIAS_MATCH

(?<=from)(?:\s)+([._[\P\\{Z}&&\P\\{Cc}&&\P\\{Cf}&&\P\\{P}]]+)(?:\sas)*(?:\s)+(?!(?:where|group\s*by|order\s*by))(\w+)

负责不正确的别名提取。我的情况的解决方案是重写 native 查询,因此它与提供的正则表达式不匹配。

关于java - Spring jpa native 查询排序添加前缀以按字段名称排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50475700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com