Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve id of last inserted object in groovy

Tags:

I am importing raw data in groovy, hundreds of thousands of entries. I use the table fields as the keys of a hash map and then use the add(hash) method on a groove sql Dataset. The Dataset goes to a postgres table and the ID field is autogenerated from a sequence. I need to get the ID of each record as it is inserted.

In java + hibernate the ID gets inserted automatically inside the corresponding field of the object being persisted. In this case the add() method does not return anything nor does it add an id field to the hash table. I am trying to avoid using hibernate/gorm here for efficiency reason.

Thanks for any pointers or for a better approach.

like image 221
Marius Seritan Avatar asked Jul 20 '09 04:07

Marius Seritan


1 Answers

groovy.sql.SQL has an executeInsert() query which returns a list of the auto-generated column values for each inserted row.

like image 152
Sean A.O. Harney Avatar answered Nov 15 '22 05:11

Sean A.O. Harney