title: Howto return ids on Inserts with Ibatis ( with RETURNING keyword )
body: <p>I'm using iBatis/Java and Postgres 8.3.
  When I do an insert in ibatis i need the id returned.<br>
  I use the following table for describing my question:<br>
  <code>CREATE TABLE sometable ( id serial NOT NULL, somefield VARCHAR(10) );</code><br>
  The Sequence <code>sometable_id_seq</code> gets autogenerated by running the create statement.  </p>
  
  <p>At the moment i use the following sql map:</p>
  
  <pre><code>&lt;insert id="insertValue" parameterClass="string" &gt;
   INSERT INTO sometable ( somefield ) VALUES ( #value# );
   &lt;selectKey keyProperty="id" resultClass="int"&gt;
    SELECT last_value AS id FROM sometable_id_seq
   &lt;/selectKey&gt;
  &lt;/insert&gt;
  </code></pre>
  
  <p>It seems this is the ibatis way of retrieving the newly inserted id. Ibatis first runs a INSERT statement and afterwards it asks the sequence for the last id.<br>
  I have doubts that this will work with many concurrent inserts. ( <a href="https://stackoverflow.com/questions/1769595/concurrency-issues-when-retriveing-ids-of-newly-inserted-rows-with-ibatis">discussed in this question</a> )</p>
  
  <p>I'd like to use the following statement with ibatis:<br>
  <code>INSERT INTO sometable ( somefield ) VALUES ( #value# ) RETURNING id;</code></p>
  
  <p>But when i try to use it within a <code>&lt;insert&gt;</code> sqlMap ibatis does not return the id. It seems to need the <code>&lt;selectKey&gt;</code> tag.</p>
  
  <p>So here comes the question:  </p>
  
  <p>How can i use the above statement with ibatis?</p>