我在 grails (2.0.3) 应用程序中使用插件 mongodb-morphia (0.7.8) 并尝试使用 Map 类型的问题。我想在我的数据库中存储一个 Map 类型的地图,但是当我把它放在我的 groovy 文件中时:
class ServiceInfo {
String name
Map<String,?> args
Date dateCreated // autoset by plugin
Date lastUpdated // autoset by plugin
static constraints = {
name nullable:false
}
}
我收到以下错误:
2012-04-29 14:39:43,876 [pool-2-thread-3] 错误 MongodbMorphiaGrailsPlugin - 处理 mongodb 域 Artefact > fr.unice.i3s.modalis.yourcast.provider.groovy.ServiceInfo 时出错:未知类型...漂亮糟糕......呼救,挥手......是的!
我试图在我的文件中指定 Map :
Map args
在这种情况下,我会收到以下简单警告:
信息:MapKeyDifferentFromString 抱怨 fr.unice.i3s.modalis.yourcast.provider.groovy.ServiceInfo.args :地图不能由对象(地图)键入;使用受支持的参数化类型(地图)
当我尝试保存一个对象时,属性 args 在数据库中被简单地省略了。有关信息,我的对象具有这种表示形式:
def icalReader= new ServiceInfo(name:"IcalReader", args:['uri':DEFAULT_URL, 'path':"fr.unice.i3s.modalis.yourcast.sources.calendar.icalreader/"])
icalReader.save()
最后,如果我只是说 args 是一个 List:
List args
我将我的对象更改为只包含一个地图的列表,我只是有一个警告:
注意:多值字段“fr.unice.i3s.modalis.yourcast.provider.groovy.ServiceInfo.args”是一个可能的异构集合。无法验证。请声明一个有效类型以消除此警告。无效的
但一切正常,我的地图已正确存储在数据库中:
{“_id”:ObjectId(“4f9be39f0364bf4002cd48ad”),“名称”:“IcalReader”,“args”:[{“路径”:“fr.unice.i3s.modalis.yourcast.sources.calendar.icalreader/”,“ uri" : "http://localhost:8080/" } ], "dateCreated" : ISODate("2012-04-28T12:33:35.838Z"), "lastUpdated" : ISODate("2012-04-28T12:33 :35.838Z") }
那么在定义我的地图时有什么我忘记的吗?我的服务确实有效,但我不喜欢“将地图封装在列表中以对其进行序列化”之类的技巧;)