在Grails中的DataBindingLazyMetaPropertyMap中,“put,get”方法确实有效,但删除不起作用。有没有人有任何想法以及解决方案???
我的代码:
def mapObj = [age:"20",location:"earth"]
mapObj.put("name","test"); // inserts the data in mapObj with key = "name"
mapObj.get("name"); // returns the value of the mapObj with key = "name"
mapObj.remove("name"); // removes the key value pair from mapObj with key = "name"
mapObj的类是java.util.LinkedHashMap
直到现在,everythig工作正常。
mapObj = domainObj.properties
将mapObj的类转换为DataBindingLazyMetaPropertyMap
mapObj.put("name","test"); // inserts the data in mapObj with key "name"
mapObj.get("name"); // returns the value of the mapObj with key "name"
mapObj.remove("name"); // returns error Method remove(Object o) is not supported by this implementation
答案 0 :(得分:1)
如果您不确定它是什么数据类型:
mapObj = domainObj.properties
println "object is now ${mapObj.getClass()}"
在groovy中,有时候事物会变成真正的物体,所以你需要复制它。比较上面例如:
mapObj = domainObj.properties.clone()
<强> E2A 强>
所以你的评论表明它无法被克隆。
请查看此链接
http://docs.grails.org/latest/ref/Domain%20Classes/properties.html
def b = new Book(title: "The Shining")
b.properties = params
b.save()
我从哪里使用过属性来查询/获取域对象图始终设置它的值
您需要编写执行select new map(e.a as a, e.b as b) from A blah
的HQL查询以返回平面地图
或在您的域类中引入新功能
Class Example
String a
String b
def loadValues() {
Map map = [:]
map.a=this.a
map.b=this.ba
return map
}
}
现在打电话
mapObj = domainObj.loadValues()
返回对象的平面地图
另请参阅implements cloneable