我试图在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
功能。
答案 0 :(得分:0)
可能是GORM MongoDB问题。您可以通过复制问题创建问题here。
但就目前而言,您可以使用List
这样解决此问题:
class User {
String name
List<String> friends = []
void removeDuplicate() {
this.friends?.unique()
}
def beforeInsert() {
removeDuplicate()
}
def beforeUpdate() {
removeDuplicate()
}
}