2012-08-08 71 views
2

鉴于这些领域类:如何,坚持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]

为什么这会失败,并有办法让它工作?

回答

1

我可以给你一个小建议(=。在update行动检查params在调试模式(如果你还没有产生的意见和控制器控制台generate-all Person域类运行)。看看到params.aliases你会发现它作为ID列表。试试相同的。