简体   繁体   English

Spring jpa 本机查询排序添加前缀以按字段名称排序

[英]Spring jpa native query sorting adding prefix to order by field name

I have problem with sorting.我有排序问题。

Repository method:存储库方法:

@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);

Pageable object:可分页 object:

Page request [number: 0, size 20, sort: name: DESC]

Hibernate log: 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 ?

I don't understand where R.name prefix came from, in the order by part in Hibernate (towards the end).我不明白R.name前缀是从哪里来的,按照Hibernateorder by (接近尾声)。

For reference, I am using:作为参考,我正在使用:

spring-data-jpa version 2.0.7.RELEASE spring-data-jpa 版本 2.0.7.RELEASE

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

UPDATE更新

I have solved this by changing the query from the native query to jpa query and it's working.我通过将查询从本机查询更改为 jpa 查询解决了这个问题并且它正在运行。 And I changed cartesian to join version.我改变了笛卡尔加入版本。

        @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);

This may be a little late to answer this question.现在回答这个问题可能有点晚了。 But thought to share how I got around this issue.但想分享我是如何解决这个问题的。 For native queries, it seems like hibernate tries to use the alias of the first table used in the query when it applies the sorting criteria.对于本机查询,似乎 hibernate 在应用排序条件时尝试使用查询中使用的第一个表的别名。 In your case the first table alias is R hence you see R.name desc in the query generated by hibernate.在您的情况下,第一个表别名是R因此您会在 hibernate 生成的查询中看到R.name desc

One way to get around this issue is to wrap you query in a select clause and name it as R like解决此问题的一种方法是将查询包装在 select 子句中并将其命名为R like

"SELECT * FROM(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 ) R"

This way at runtime hibernate would apply the sort criteria on top of your query which corresponds to R now.这种方式在运行时休眠将在您的查询之上应用排序标准,现在与R相对应。

It has Sort class for this you can use this maybe.它有 Sort 类,你可以使用它。 Besides, it is easy to use.此外,它易于使用。

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.sorting https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.sorting

here is jira ticket with more details which can be key for resolution ( https://jira.spring.io/browse/DATAJPA-1613 ).这是带有更多细节的 jira 票,这可能是解决问题的关键( https://jira.spring.io/browse/DATAJPA-1613 )。

QueryUtils.ALIAS_MATCH QueryUtils.ALIAS_MATCH

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

responsible to incorrect alias extraction.负责不正确的别名提取。 The solution for my case was rewrite native query, so it doesn't match the provided regexp.我的情况的解决方案是重写本机查询,因此它与提供的正则表达式不匹配。

I faced similar issue especially in case of complex queries where there is ORDER BY with in the query.我遇到了类似的问题,尤其是在查询中包含ORDER BY的复杂查询的情况下。 I was getting syntax error because a , was getting added before ORDER BY .我收到语法错误,因为在ORDER BY之前添加了,

The way I solved this issue was to create a VIEW with the SELECT query having necessary fields required for result set and WHERE condition (so you can run query with params in WHERE condition against the VIEW ).我解决此问题的方法是使用SELECT查询创建一个VIEW ,该查询具有结果集和WHERE条件所需的必要字段(因此您可以在WHERE条件下针对VIEW使用参数运行查询)。 And write native query to SELECT FROM the VIEWFROM VIEW将本机查询写入SELECT

CREATE VIEW my_view AS (// your complex select query with required fields);

@Query("SELECT field1 AS alias1, field2  AS alias2 FROM my_view  "
        + "WHERE field3  = :param1 AND field4 = :param2")
Page<MyDto> myFunction(@Param("param1") Long param1, @Param("param1") String param2, Pageable pageable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM