5

I've got a postgres table where the ID is defined as bigserial. How can I use @Insert and get back the id of the inserted entity? I am expecting the mapper method to either return the id or populate the id field inside the entity object. Any ideas?

Gili
  • 86,244
  • 97
  • 390
  • 689
  • For some reason this doesn't work me on mysql :( http://stackoverflow.com/questions/4283159/howto-return-ids-on-inserts-with-mybatis-in-mysql-with-annotations – ripper234 Nov 26 '10 at 07:21

2 Answers2

4

The mapper will return you the number of records that were actually inserted. In order to get back the id of the inserted record, you'll need to add a second annotation (that will populate the id) :

@Options(useGeneratedKeys=true, keyProperty="idSomething")

Note that keyProperty is not necessary if the identifiyng property is named "id" in your entity object.

Manur
  • 8,436
  • 2
  • 27
  • 29
3

NVM, i think i found the answer on the other thread, http://mybatis-user.963551.n3.nabble.com/How-to-return-the-ID-of-the-inserted-object-under-Postgres-td1926959.html

There's the link for anyone else who lands here.

user519298
  • 46
  • 1