Grails数据绑定中的黑魔法?

时间:2010-04-08 11:18:06

标签: spring data-binding rest grails groovy

http://n4.nabble.com/Grails-Data-Binding-for-One-To-Many-Relationships-with-REST-tp1754571p1754571.html中所述,我正在尝试自动绑定我的REST数据。

我现在明白,对于一对多关联,数据绑定所需的地图必须包含多方的ID列表,例如:

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

但是,我得到了这个例外

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

然而,即使是更奇怪的是为控制器生成的更新操作。我们有像这样的数据绑定:

domainObjectInstance.properties = params['domainObject']

但是,这是非常奇怪的事情,params ['domainObject']为空!它为null,因为所有domainObject字段都直接在params映射中传递。如果我将上面的行更改为

domainObjectInstance.properties = null

domainObject仍在更新!为什么会发生这种情况,更重要的是,如果它以这种格式出现,我怎样才能自动绑定传入的XML(问题是一对多关联):

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

1 个答案:

答案 0 :(得分:0)

你的例子并不是100%清楚 - 当你试图绑定时,你的“params”的内容究竟是什么。

如果你没有定义“domainObject.xxx = yyy”帖子参数,那么params ['domainObject']中就没有任何内容。在大多数情况下,这样的命名空间不是必需的,并且要求表单字段使用前缀。

我猜你的数据是以“xxx = yyy”而不是“domainObject.xxx = yyy”进行的。

此外,我不确定关联的[1,2]是否正确。当然应该是[id:1,id:2]?