views:

226

answers:

1

As described in http://n4.nabble.com/Grails-Data-Binding-for-One-To-Many-Relationships-with-REST-tp1754571p1754571.html i'm trying to automatically bind my REST data.

I understand now that for one-to-many associations the map that is required for the data binding must have a list of ids of the many side such as:

[propName: propValue, manyAssoc: [1, 2]]

However, I'm getting this exception

Executing action [save] of controller [com.example.DomainName]  caused exception: org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred calling getter of com.example.DomainName.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.example.DomainName.id

However, even weirder is the update action that is generated for the controller. There we have the databinding like this:

domainObjectInstance.properties = params['domainObject']

But, and this is the really weird thing, params['domainObject'] is null! It is null because all the domainObject fields are passed directly in the params map itself. If I change the above line to

domainObjectInstance.properties = null

the domainObject is still updated! Why is this happening and more important, how can I bind my incoming XML automatically if it comes in this format (the problem is the one-to-many associations):

<product>
  <name>Table</name>
  <brand id="1" />
  <categories>
    <category id="1" />
    <category id="2" />
  </categories>
</product> 
A: 

It's not 100% clear from your example - what exactly is the contents of your "params" when you try to bind.

If you don't have "domainObject.xxx=yyy" post parameters defined, then nothing will be in params['domainObject']. Namespacing like that is not necessary in most cases, and required the form fields to use the prefix.

I'm guessing your data is coming in as "xxx=yyy" not "domainObject.xxx=yyy".

Furthermore, I'm not sure that [1,2] for associations is right either. Surely that should be [id:1, id:2] ?

Marc Palmer