开发者

Grails: Saving nested objects using XML data binding

I'm creating a REST service in Grails to accept data from a python script. The python script generates an XML representation of the object graph and submits it to the controller. Things work great for my flat objects, but I can't figure out how to handle the case where a domain object contains a Set of children objects. For unrelated reasons, my DOA layer is pure Java JPA.

For example, my domain classes (Leaving out getters/setters/etc):

class Schedule {
  String name;
  @OneToMany;
  HashSet&开发者_C百科lt;Step> steps;
}

class Step {
    String name;
    @ManyToOne;
    Schedule schedule;
}

My python script generates XML like the following:

<schedule>
  <name>Foo</name>
  <steps>
     <step>
       <name>Bar</name>
     </step>
     <step>
        <name>Blatz</name>
     </step>
  </steps>
</schedule>

In my controller I have this:

def save = {
   def schedInstance = new Schedule(params['schedule'])
   ...
}

The steps property never gets populated. If I dump params out to a log the steps data is all jammed together (In my example above it would yield steps: "BarBlatz"

I have to be doing something terribly wrong. I would imagine that this is a common task. Everything I've been able to find about nesting objects is related to command objects. I don't want to have to duplicate my domain object code in a command object if I can avoid it.


def save = {
    def schedInstance = new Schedule(params['schedule'])
    params['schedule?.step'].each{stepXml->
        schedInstance.addTo(new step(stepXml)
    }
}

Untested, but I've done something similar before . I'll edit this as needed as soon as I have a chance to test it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜