Spring引导应用程序中的UnuniquereSultException

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

在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 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

我找不到任何文档,但有一篇关于此主题的博客文章说:

  • 能够按名称和(原始)参数类型查找方法
  • 名为…All(…)的方法影响项集合和/或返回集合
  • 采用标识符的方法命名为…ById(…)。4让我们放弃ID扩展可序列化的要求

  • 来源:

    能否设置logging.level.org.hibernate.SQL=debug并发布生成的SQL语句?@SimonMartinelli是的。它记录了我在注释中的确切查询。这只是一个猜测,但是你能把你的方法命名为findallclors()吗?这是一个非常好的猜测!如果你写一个答案,我会做正确的标记(说明原因的额外分数…)完成。谢谢你接受我的回答。我很乐意帮忙