user1698253
user1698253

Reputation: 55

How to save a collection all at once in Grails

For example I need to retrieve several registers in a table, and edit a field, but it takes too long to save all with a loop, does exist a better way to save? This how I do it....

class Table

   static mapping = {
      table "TABLEEX"
      id generator:'sequence', params:[sequence:'TABLEEX_SEQ']
   }

   // identificacion
   String data1
   String data2
}

And searching the data:

def stuff = Table.createCriteria().list{
   eq("data1","1")
}

And editing and saving

stuff.each {
   it.data2 = "aaa"
   it.save()
}

Upvotes: 0

Views: 1169

Answers (2)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27200

It isn't clear why you are retrieving the objects to begin with. Is something like this what you are looking for?

Table.executeUpdate("update Table t set t.data2=:newData where t.data1=:oldData", [newData: 'BAR', oldData: 'FOO'])

EDIT

You could also do something like this...

def query = Table.where {
    data1 == 'FOO'
}
int total = query.updateAll(data2:'BAR')

Upvotes: 6

Vitor Hugo
Vitor Hugo

Reputation: 1126

Hibernate (the underlying mechanism of gorm, the grails orm) does not support that. You'll have to iterate over every element and save or implement it yourself (and that will not make it faster).

Upvotes: -3

Related Questions