You would do this (or close): <insert id="insert"> INSERT INTO person (lastname, firstname) values (#lastname#, #firstname#) <selectKey property="personId">SELECT curval('persons_id_seq')</selectKey> </insert>
After calling the insert method, your bean (or map) would have the personId property set. Larry On 9/30/05, Alan Chandler <[EMAIL PROTECTED]> wrote: > I am not really sure I understand fully what selectKey is trying to do. > > My situation is this. > > I have a postgres table "PERSONS" with a primary key of ID which is of type > SERIAL. In Postgres this means an automatic creation of a SEQUENCE, and a > default value in the column of nextval('persons_id_seq') > > I want to create the transaction to insert a new person into the PERSONS > table, but I need to know the ID that it has been created with, so that I can > use that. > > There are two options (I think) from a SQL point of view > > 1) Within a single transaction > > SELECT nextval('persons_id_seq') > > - which will output a single value - and which I can pick up in a resultclass > of int using iBatis, then > > INSERT INTO persons (id , ...) values (#id#,...) where id is the value > returned in the int > > 2) Within a single transaction > > INSERT INTO persons (data but no the id) > SELECT curval('persons_id_seq') > > and use the value returned from select statement as the ID of the inserted > row. > > > > Where does selectKey fit into this - does it perfom the attached select with > the insert automatically within the same transaction without manually needed > to define the transaction boundaries? What else does it do? > > How to I get back the ID from the API when I call the SqlMap.insert routine. > > -- > Alan Chandler > http://www.chandlerfamily.org.uk >