英文:
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 = "SELECT t FROM trainings t ORDER BY RANDOM() LIMIT 8", nativeQuery = true)
List<Training> 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
专注分享java语言的经验与见解,让所有开发者获益!
评论