“addAll()”的语法在Grails中列出? [英] Syntax for "addAll()" to list in grails?
问题描述
在java中,如果我有一个列表,我可以使用 addAll(otherList);
将所有元素从一个列表添加到另一个列表中。
Grails中的等价物是什么?我有一个具有hasMany关系的Domain对象。要添加到它,我会使用类似于
Object.addToMyList(someitem);
并且看起来像
Object.addAllToMyList(otherList)
不存在。
在grails中的等价物是什么?
澄清 - 默认情况下,集合是设置
,但 addAll()
可以与任何集合
一起使用。
你可以调用 addAll()
,它可以正常工作,但是如果返回引用是双向的,它将不会被设置。这不会影响持久性,只是当前的内存状态。
GORM没有内置任何内容,因此我认为正确方式是循环,例如
otherItems.each {foo.addToBars(it)}
In java, if I have a list, I can use addAll(otherList);
to add all the elements from one list to another.
What is the equivalent in grails? I have a Domain object with a hasMany relationship. To add to it, I would use something like
Object.addToMyList(someitem);
and it seems like
Object.addAllToMyList(otherList)
does not exist. What is the equivalent in grails?
To clarify - by default the collection is a Set
, but addAll()
works with any Collection
.
You can call addAll()
and it'll work fine, although the back-references won't be set if it's bidirectional. This doesn't affect persistence, just the current in-memory state.
There's nothing built into GORM for this, so I suppose the "right" way is a loop, e.g.
otherItems.each { foo.addToBars(it) }
这篇关于“addAll()”的语法在Grails中列出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!