Grails: saving nested objects using XML data binding

I am creating a REST service in Grails to receive data from a python script. The Python script creates an XML representation of the object graph and sends it to the controller. Everything works fine for my flat objects, but I can't figure out how to handle the case when a domain object contains a set of child objects. For unrelated reasons, my DOA level is pure Java JPA.

For example, my domain classes (output from getters / seters / etc):

class Schedule { String name; @OneToMany; HashSet<Step> steps; } class Step { String name; @ManyToOne; Schedule schedule; } 

My python script generates XML as follows:

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

In my controller, I have the following:

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

The steps property is never populated. If I dump the parameters to the log, all the shutter data is stuck together (in my example above, this will lead to the steps: "BarBlatz"

I have to do something terribly wrong. I would suggest that this is a common task. Everything I managed to find about nested objects is related to command objects. I do not want to duplicate the domain object code in the command object if I can avoid it.

+4
source share
1 answer
 def save = { def schedInstance = new Schedule(params['schedule']) params['schedule?.step'].each{stepXml-> schedInstance.addTo(new step(stepXml) } } 

Unconfirmed, but I used to do something similar. I will edit this as necessary, as soon as I have the opportunity to check it.

+1
source

All Articles