【问题标题】:JPA Query gets error InvalidDataAccessResourceUsageExceptionJPA 查询出现错误 InvalidDataAccessResourceUsageException
【发布时间】:2018-10-18 04:05:46
【问题描述】:

我写了一个查询

public interface TeabagRepository extends CrudRepository<Teabag, Long> {

    @Query(value = "SELECT t.post, t.status, t.expires FROM teabags t WHERE t.status = 'hot' AND t.user_id = ?1", nativeQuery = true)
    Teabag findTea(Long id);
}

我在application.properties中设置了spring.jpa.show-sql=true

所以查询显示在控制台中,我在数据库中运行了这个查询并且它有效:

SELECT
    t.post,
    t.status,
    t.expires 
FROM
    teabags t 
WHERE
    t.status = 'hot' 
    AND t.user_id = ?

错误:

2018-05-08 19:22:27.675 错误 1259 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] :Servlet.service() for 带有路径 [] 的上下文中的 servlet [dispatcherServlet] 引发异常 [请求处理失败;嵌套异常是 org.springframework.dao.InvalidDataAccessResourceUsageException:可以 不执行查询; SQL [SELECT t.post, t.status, t.expires FROM teabags t WHERE t.status = 'hot' AND t.user_id = ?];嵌套的 异常是 org.hibernate.exception.SQLGrammarException: could not 执行查询] ****有根本原因

org.postgresql.util.PSQLException: 列名 id 未在 这个 ResultSet.****

org.springframework.dao.InvalidDataAccessResourceUsageException: 可以 不执行查询; SQL [SELECT t.post, t.status, t.expires FROM 茶包 t WHERE t.status = 'hot' AND t.user_id = ?];

更新:更多错误文字

嵌套异常是 org.hibernate.exception.SQLGrammarException: could not execute query

根错误

有根本原因

org.postgresql.util.PSQLException: 列名 id 未在 这个结果集。

为什么这个查询在 Spring Boot 应用程序中不起作用?

【问题讨论】:

  • 根本原因是什么?还有另一个导致异常,其中包含实际原因。
  • 我不确定根在哪里可能是这样的: 原因:org.postgresql.util.PSQLException:在此 ResultSet 中找不到列名 id。
  • 但是如果无法执行查询,不要认为结果集与错误有关,因为没有结果
  • 如果没有完整的堆栈跟踪和必要的信息来逐个分析发生的事情,我很难正确诊断原因。
  • 这就是所有的错误..

标签: spring-data-jpa


【解决方案1】:

结果是需要 Teabag 的每一列,所以我不得不选择 t.*

@Query(value = "SELECT t.* FROM teabags t WHERE t.status = 'hot' AND t.user_id = ?1", nativeQuery = true)
    Teabag findTea(Long id);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    相关资源
    最近更新 更多