简体   繁体   English

具有一对多关系的Grails数据绑定-链接回父级的链接丢失

[英]Grails data binding with one-to-many relationship - link back to parent is lost

In a Grails 2.2.2 application, I have a one-to-many relationship that looks something like this: 在Grails 2.2.2应用程序中,我具有一对多关系,看起来像这样:

Parent: 上级:

class Client {
    ...
    String name
    static hasMany = [modelFieldInstances: ModelFieldInstance]
    ...
}

Child: 儿童:

class ModelFieldInstance {
    ...
    String name
    String value
    static belongsTo = [client: Client]
    ...
}

I'm trying to create a data importer so that users can import a spreadsheet or csv containing their client records. 我正在尝试创建一个数据导入器,以便用户可以导入包含其客户记录的电子表格或csv。 To do this, I inspected the parameters that grails scaffolding uses when it creates a new Client instance in the save method of the standard scaffolding controller. 为此,我检查了当脚手架在标准脚手架控制器的save方法中创建新的Client实例时使用的参数。

The problem is that when I try to create and save a new Client instance with my importer, the child ModelFieldInstance s are saved without their reference to the parent Client (the Client and the ModelFieldInstance s all get persisted though). 问题是,当我尝试使用导入程序创建并保存新的Client实例时,子ModelFieldInstance的保存没有引用父Client的引用(尽管ClientModelFieldInstanceModelFieldInstance )。

In my importer, I'm doing the data binding like this: 在我的导入程序中,我正在像这样进行数据绑定:

Client client = new Client()
client.modelFieldInstances = ListUtils.lazyList(new ArrayList(), {new ModelFieldInstance()} as org.apache.commons.collections.Factory)
client.properties = properties
...
client.save()

I think the only real difference between how the Grails scaffolding controller works and how my importer works is that in my importer I'm initially setting the modelFieldInstances collection as a LazyList . 我认为Grails脚手架控制器的工作方式与导入器的工作方式之间唯一真正的区别是,在导入器中,我最初将modelFieldInstances集合设置为LazyList However, before I added the LazyList assignment the data binding was blowing with an error like this: 但是,在我添加LazyList分配之前,数据绑定因以下错误而LazyList

Invalid property 'modelFieldInstances[31]' of bean class [com.myapp.Client]: Illegal attempt to get property 'modelFieldInstances' threw exception; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'modelFieldInstances[31]' of bean class [com.myapp.Client]: Cannot get element with index 31 from Set of size 0, accessed using property path 'modelFieldInstances[31]'**

So, I guess the question is, why doesn't the data binding work in my data importer when I can see that it does work in the scaffolding controller for a given properties map. 因此,我想问题是,当我看到数据绑定在给定的属性映射的脚手架控制器中确实起作用时,为什么数据绑定在我的数据导入器中不起作用。

As a sort of work around, I implemented a method that iterates over all the items in the modelFieldInstances collection and manually assigns the parent ( client ) to each one. 作为一种变通方法,我实现了一种方法,该方法可对modelFieldInstances集合中的所有项目进行迭代,并手动将父项( client )分配给每个项。 This seems to work fine, however I'm still unsure as to why grails data binding doesn't do this for me. 这似乎工作正常,但是我仍然不确定为什么grails数据绑定对我没有做到这一点。 I wonder if there's some magic happening in GrailsParameterMap ... 我想知道GrailsParameterMap是否发生了一些魔术...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM