Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for "addAll()" to list in grails?

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?

like image 632
Derek Avatar asked Oct 19 '25 06:10

Derek


1 Answers

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) }
like image 116
Burt Beckwith Avatar answered Oct 21 '25 21:10

Burt Beckwith