【问题标题】:Spring Boot doesn't work custom JPA requestSpring Boot 不起作用自定义 JPA 请求
【发布时间】:2020-04-10 11:26:59
【问题描述】:

拥有此代码:

@Query(value = "SELECT t FROM trainings t ORDER BY RANDOM() LIMIT 8", nativeQuery = true)
List<Training> findRandom();

收到此错误:

org.postgresql.util.PSQLException: The column name id was not found in this ResultSet.

执行此代码时:

System.out.println(trainingRepo.findRandom());

我的问题在哪里?我该如何解决?

【问题讨论】:

  • 你在 db lvl 上测试过这个查询吗?
  • 不返回id字段?你为什么不直接做select *

标签: java spring postgresql spring-boot


【解决方案1】:

你不需要在这里使用“t”,因为它不是 JPQL,而是原生查询(你已经设置了nativeQuery = true

将查询替换为SELECT * FROM trainings ORDER BY RANDOM() LIMIT 8

【讨论】:

  • 或者将SELECT t替换为SELECT t.*
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 2020-07-11
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 2019-12-27
  • 1970-01-01
相关资源
最近更新 更多