给定这些域类:
class Person {
Long id
String name
static hasMany = [aliases: PersonAlias]
}
class PersonAlias {
Person person
Long id
String name
}
我尝试通过JSON区域进行直接的往返加载/保存:
Person p = Person.get(20005353);
def json = p as JSON
def str = json as String
def map = JSON.parse(str)
p.properties = map
p.save(flush:true)
产生这个JSON
{
"id": 20005353,
"name": "John Smith",
"class": "Person",
"aliases":
[
{
"class": "PersonAlias",
"id": 99,
"name": "J. Smith"
}
]
}
并因以下错误而失败:
grails.validation.ValidationErrors: 1 errors Field error in object 'heavymeta.Person' on field 'aliases': rejected value [[]]; codes [typeMismatch.heavymeta.Person.aliases,typeMismatch.aliases,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [heavymeta.Person.aliases,aliases]; arguments []; default message [aliases]]; default message [Failed to convert property value of type 'org.codehaus.groovy.grails.web.json.JSONArray' to required type 'java.util.Set' for property 'aliases'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.util.LinkedHashMap] to required type [heavymeta.PersonAlias] for property 'aliases[0]': no matching editors or conversion strategy found]
为什么这会失败,并且有办法使其起作用?
请您参考如下方法:
我可以给您一些建议(=。在params操作中以 Debug模式检查update(如果尚未生成在控制台generate-all Person中运行的域类的 View 和 Controller )。看一看params.aliases,您会在列表中找到它ID。尝试相同的方法。




