如何格式化Postgres的spring-data-jpa本机查询

时间:2018-10-17 19:13:01

标签: spring-data-jpa nativequery

我正在尝试使用以下方法从jpa存储库中查询postgres数据库:

@Query(value =
    "SELECT " +
        "string_agg(distinct customer_po, ', ') as customer_po, " +
        "customer_part, " +
        "description, " +
        "bunzl_loc, " +
        "sum(dayspastexpected) as longestGrouping, " +
        "string_agg(distinct priority, ', '), " +
        "sum(open_qty) as totalBoQty, " +
        "sum(current_oh) as sunAvail, " +
        "next_receipt " +
    "FROM " +
        "sap_data.openorders_report " +
    "WHERE " +
        "run_date between :from and :to " +
    "AND " +
        "backorderorfuture = 'Future' " +
    "GROUP BY " +
        "customer_part, " +
        "bunzl_loc, " +
        "description, " +
        "next_receipt " +
    "ORDER BY " +
        "customer_part desc, " +
        "bunzl_loc desc /*#pageable*/",
    nativeQuery = true)
Page<SapOpenOrder> getBackOrderReport(@Param("from") Date from, @Param("to") Date to, Pageable pageable);

但我遇到此错误:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "WHERE"

我可以在dbeaver中运行此查询并获得正确的结果。如何使用spring jpa运行此查询?

1 个答案:

答案 0 :(得分:0)

如果还没有,请更新到Spring Data JPA的最新版本。 然后删除/*#pageable*/。 可以解决Spring Data JPA中的一个错误,该错误现已修复。 有关详细信息,请参见https://jira.spring.io/browse/DATAJPA-928

如果这不能解决问题,请使用实际执行的SQL语句更新您的问题。 在为您的JPA实现配置日志记录之后,您可以从日志中获取该语句。