首页 > 解决方案 > JPA 查询出现错误 InvalidDataAccessResourceUsageException

问题描述

我写了一个查询

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

spring.jpa.show-sql=true进去了application.properties

所以查询显示在控制台中,我在数据库中运行了这个查询,它可以工作:

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] oaccC[.[.[/].[dispatcherServlet]:Servlet.service() 用于 servlet [dispatcherServlet] path [] 抛出异常 [请求处理失败;嵌套异常是 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 execute query] ****with root cause

org.postgresql.util.PSQLException:在此 ResultSet 中找不到列名 id。****

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:无法执行查询

根错误

有根本原因

org.postgresql.util.PSQLException:在此 ResultSet 中找不到列名 id。

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

标签: spring-data-jpa

解决方案


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

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

推荐阅读