英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论