DataBindingLazyMetaPropertyMap

DataBindingLazyMetaPropertyMap

在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

到现在为止,所有人都工作正常。
 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

最佳答案

不确定时,请检查数据类型:

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

09-27 05:46