【问题标题】:Spring Boot, MongDB and KotlinSpring Boot、MongoDB 和 Kotlin
【发布时间】:2019-09-02 16:01:01
【问题描述】:

我正在尝试在我的文档中添加一个唯一的复合键,如下所示:

@Document
@CompoundIndexes({
    CompoundIndex(def = "{'firstName':1, 'lastName':1}", name = "compound_index_1", unique = true)
})

但我得到了错误:

An annotation argument must be a compile-time constant.

谁能帮帮我?

【问题讨论】:

    标签: mongodb spring-boot kotlin


    【解决方案1】:

    在 Kotlin 中,数组以不同的方式传递给注解。查看docs on kotlinlang,您会在底部附近看到这个sn-p:

    // Kotlin 1.2+:
    @AnnWithArrayMethod(names = ["abc", "foo", "bar"]) 
    class C
    
    // Older Kotlin versions:
    @AnnWithArrayMethod(names = arrayOf("abc", "foo", "bar")) 
    class D
    

    所以你的花括号在这里不起作用,你需要方括号。

    @Document
    @CompoundIndexes(value = [
        CompoundIndex(def = "{'firstName':1, 'lastName':1}",
                      name = "compound_index_1", unique = true)
        ])
    

    【讨论】:

      猜你喜欢
      • 2018-02-07
      • 2017-04-10
      • 2016-02-22
      • 2015-12-08
      • 2020-09-17
      • 2017-05-28
      • 2017-02-23
      • 2017-11-14
      • 1970-01-01
      相关资源
      最近更新 更多