ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alan Chandler <a...@chandlerfamily.org.uk>
Subject Trying to understand selectKey
Date Fri, 30 Sep 2005 17:19:54 GMT
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 

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

View raw message