Pav*_*nir 6 java sorting nativequery spring-data-jpa
我的排序有问题。
存储库方法:
@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);
Run Code Online (Sandbox Code Playgroud)
可分页对象:
Page request [number: 0, size 20, sort: name: DESC]
Run Code Online (Sandbox Code Playgroud)
休眠日志:
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 ?
Run Code Online (Sandbox Code Playgroud)
我不明白(最后)部分R.name中的前缀来自哪里。order byHibernate
作为参考,我正在使用:
spring-data-jpa 版本 2.0.7.RELEASE
spring-boot-starter-data-jpa 版本 2.0.2.RELEASE
更新
我已经通过将查询从本机查询更改为 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);
Run Code Online (Sandbox Code Playgroud)
它有 Sort 类,您也许可以使用它。此外,它很容易使用。
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.sorting