Spring引导应用程序中的UnuniquereSultException
在Spring Boot/Kotlin应用程序中,我有以下存储库:Spring引导应用程序中的UnuniquereSultException,spring,spring-boot,jpa,kotlin,Spring,Spring Boot,Jpa,Kotlin,在Spring Boot/Kotlin应用程序中,我有以下存储库: interface CatRepository : CrudRepository<Cat, Long> { @Query(value = "SELECT DISTINCT c.color FROM cat c", nativeQuery = true) fun findColors(): List<String> } 然后在我的模板中,我有以下内容: @Controller class
interface CatRepository : CrudRepository<Cat, Long> {
@Query(value = "SELECT DISTINCT c.color FROM cat c", nativeQuery = true)
fun findColors(): List<String>
}
然后在我的模板中,我有以下内容:
@Controller
class HtmlController(private val repository: CatRepository) {
@GetMapping("/")
fun index(model: Model): String {
model["colors"] = repository.findColors()
return "index"
}
}
<select name="color">
{{#colors}}
<option value="">{{.}}</option>
{{/colors}}
</select>
{{{颜色}
{{.}}
{{/colors}
当数据库的cat
表中只有一个cat
时,这就起作用了。当我添加另一个时,会出现以下错误:
UnuniqueresultException:查询未返回唯一结果:2
我哪里出错了?说出你的方法
findAllColors()
告诉Spring数据返回类型为collection
我找不到任何文档,但有一篇关于此主题的博客文章说:
来源:能否设置logging.level.org.hibernate.SQL=debug并发布生成的SQL语句?@SimonMartinelli是的。它记录了我在注释中的确切查询。这只是一个猜测,但是你能把你的方法命名为findallclors()吗?这是一个非常好的猜测!如果你写一个答案,我会做正确的标记(说明原因的额外分数…)完成。谢谢你接受我的回答。我很乐意帮忙