“Spring Boot不起作用的自定义JPA请求”

huangapple go评论72阅读模式
英文:

Spring Boot doesn't work custom JPA request

问题

有这段代码:

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

出现了以下错误:

org.postgresql.util.PSQLException: 在此结果集中找不到列名 id

当执行以下代码时:

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

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

英文:

Having this code:

@Query(value = &quot;SELECT t FROM trainings t ORDER BY RANDOM() LIMIT 8&quot;, nativeQuery = true)
List&lt;Training&gt; findRandom();

Getting this error:

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

When executing this code:

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

Where is my problem? How can I solve it?

答案1

得分: 2

你不需要在这里使用“t”,因为这不是JPQL,而是本地查询(你已将nativeQuery = true设置)

使用以下查询替换:SELECT * FROM trainings ORDER BY RANDOM() LIMIT 8

英文:

You don't need to use "t" here because it's not JPQL but native query (you've set nativeQuery = true)

Replace the query with SELECT * FROM trainings ORDER BY RANDOM() LIMIT 8

huangapple
  • 本文由 发表于 2020年4月10日 19:26:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61139315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定