2

我正在尝试在 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.

将 Set 更改为 List 可以正常工作,但我不想重复,也不想用 List 来管理它。

GORM 有没有办法使用底层的 Mongo$addToSet功能。

4

1 回答 1

0

这可能是一个 GORM MongoDB 问题。您可以在此处通过重现问题来创建问题。

但是现在,您可以使用以下方法解决此问题List

class User {

    String name
    List<String> friends = []

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


    def beforeInsert() {
         removeDuplicate()
    }

    def beforeUpdate() {
         removeDuplicate()
    }
}
于 2016-03-07T04:27:19.377 回答