JPA Query获取错误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);
}

我在 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 = ?

The error:

2018-05-08 19:22:27.675 ERROR 1259 --- [nio-8080-exec-4] oaccC [ . [ . [/] . [dispatcherServlet]:Servlet中的Servlet.service()[dispatcherServlet] with 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:无法执行查询] ****与根本原因org.postgresql.util.PSQLException:在此ResultSet中找不到列名称 . **** org.springframework . dao.InvalidDataAccessResourceUsageException:无法执行查询; SQL [SELECT t.post,t.status,t.expires FROM teabags t WHERE t.status ='hot'AND t.user_id =?];

update: 更多错误文字

嵌套异常是org.hibernate.exception.SQLGrammarException:无法执行查询

root error

具有根本原因org.postgresql.util.PSQLException:在此ResultSet中找不到列名称id .

为什么这个查询不能在spring启动应用程序中运行?

回答(1)

2 years ago

结果是期待茶袋的每一列,所以我必须选择t . *

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