Derek
Derek

Reputation: 11915

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?

Upvotes: 4

Views: 3395

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75671

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) }

Upvotes: 10

Related Questions