鑑於這些領域類:如何,堅持JSON到Grails的域對象嵌套成員
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]
爲什麼這會失敗,並有辦法讓它工作?