【问题标题】:Spring Boot JPA native query for call PostgreSQL function用于调用 PostgreSQL 函数的 Spring Boot JPA 本机查询
【发布时间】:2021-05-19 22:20:11
【问题描述】:

我对spring boot的SQL函数不熟悉。谁能检查我的 spring boot 查询是否正确?

我创建了一个 PostgreSQL 函数并用 spring boot 实现了它。 但是当我调用 JPA 查询时出现错误。下面我附上了SQL函数和JPA查询

PostgreSQL 函数 - 工作正常

select  pulse.fn_get_ticket_details_json(''::text,null::date,null::date,''::text)

Spring 启动查询

@Query(value = "select  pulse.fn_get_ticket_details_json(''::text,null::date,null::date,''::text)", nativeQuery = true)
JsonObject SearchData();

错误信息

 ERROR: syntax error at or near ":"



Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

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

截图

【问题讨论】:

  • 冒号被 JPA 用于替换变量。你能用\\: 转义冒号: 吗?所以,你的::text, 应该是\\:\\:text,
  • @gtiwari333 显示错误 @Query(value = "select pulse.fn_get_ticket_details_json(''::text,null::date,null::date,''\\:\\:text)" , nativeQuery = true)
  • 你必须为每次出现:做这个
  • @gtiwari333 我添加了一个截图。请检查一下。

标签: spring postgresql spring-boot spring-data-jpa


【解决方案1】:

这应该可以工作

    @Query(value = "select pulse.fn_get_ticket_details_json(''\\:\\:text,null\\:\\:date,null\\:\\:date,''\\:\\:text)", nativeQuery = true)

删除多余的空格并检查您的简单引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2018-03-17
    • 2021-03-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多