2016-03-04 14 views
2

我正尝试在Grails GORM Mongo域类中创建嵌入式集合。使用java.util.Set使用Grails 3.1.x和Mongo 5.0.x插件的域属性

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

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

当我尝试保存用户域类:

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功能。

+0

你能提供你的堆栈跟踪? –

回答

0

这可能是一个GORM MongoDB问题。您可以创建问题here并重现问题。

但现在,你可以使用List这样做解决此问题:

class User { 

    String name 
    List<String> friends = [] 

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


    def beforeInsert() { 
     removeDuplicate() 
    } 

    def beforeUpdate() { 
     removeDuplicate() 
    } 
}