使用Restful Controller在深层中将JSON保存为域类

时间:2015-03-10 19:07:12

标签: json rest grails

我有两个域类:

class A {
 String foo
 static hasMany = [children: B]
}

class B {
 String bar
 static belongsTo = [a: A]
}

我想从JSON中保存A,如:

{
 foo: "Hello world!",
 children: [
  {bar: "First child!", _ref: "../.."},
  {bar: "Second child!", _ref: "../.."}
 ]
}

(注意:如果我没有放入_ref,则保存B对象但不引用A,即空值。)

我有一个Restfull控制器,如:

class AController extends RestfullController {
 static responseFormats = ['json', 'xml']
 AController() {
  super(A)
 }
}

但是,当我试图保存我的物体时,我得到了:

ERROR errors.GrailsExceptionResolver  - TransientObjectException occurred when processing request: [POST] /example/a/save.json
object references an unsaved transient instance - save the transient instance before flushing: mx.gob.morelos.ppi.Proyecto. Stacktrace follows:
Message: object references an unsaved transient instance - save the transient instance before flushing: A
    Line | Method
->>   99 | $tt__save in grails.rest.RestfulController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    617 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

我需要做些什么来保存我们的JSON与他们的关系?

问候。

0 个答案:

没有答案