使用带有Grails 3.1.x和Mongo 5.0.x插件的java.util.Set域属性

时间:2016-03-04 23:20:22

标签: mongodb grails gorm grails-3.1

我试图在Grails GORM Mongo域类中创建嵌入式集合。

class User {
    String name
    Set<String> friends = []
}

我想存储其他用户名的Set(非重复列表)。

当我尝试保存用户域类时:

new User(name: 'Bob').save(failOnError: true)

我收到了错误。

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for interface java.util.Set.

将“设置”更改为“列表”可以正常工作,但我不想要重复,也不想使用列表进行管理。

GORM是否会使用基础Mongo $addToSet功能。

1 个答案:

答案 0 :(得分:0)

可能是GORM MongoDB问题。您可以通过复制问题创建问题here

但就目前而言,您可以使用List这样解决此问题:

class User {

    String name
    List<String> friends = []

    void removeDuplicate() {
        this.friends?.unique()
    }


    def beforeInsert() {
         removeDuplicate()
    }

    def beforeUpdate() {
         removeDuplicate()
    }
}