2016-03-04 26 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() 
    } 
}