ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Möller <dcmoel...@gmx.de>
Subject Re: select Key not working giving error.
Date Fri, 13 Jul 2007 06:49:19 GMT
felix thomas schrieb:
> hi,
> 
>    I am not able to understand if the key are not
> mapped , how is it working for you.

Before switching to iBatis I've coded it the usual JDBC-way: After
executing the INSERT I've triggered the SELECT (given in the "selectKey"
element) on the *same* Connection to retrieve the auto-incremented
value. (Some JDBC drivers support java.sql.Statement.getGeneratedKeys(),
but this does not seem the way iBatis handles this feature.)

I think iBatis handles it the same way. Think of the "selectKey" element
as something like a "select" element with the given resultClass
attribute (and without any parameterMap cause of the lack of any
parameters, of course):

  <select resultClass="long">
    select @@IDENTITY
  </select>

This SELECT could be handled just like I've decribed in my posting. That
way the usual semantics of the result of sqlMapClient.insert() changes:
You do not receive the number of updates the INSERT yields but the
result of executing the given SELECT statement.

> Can anyone give an example done in Oracle for this
> kind of situation.

Unfortunately, I do not have an Oracle DB at hand. But the iBatis
developer guide shows an example handling Oracle specifics at page 22/23.

Greetings

Christian

> --- Christian Möller <dcmoeller@gmx.de> wrote:
> 
>> Hi,
>>
>> I myself are using this kind of iBATIS config with a
>> Sybase DB:
>>
>>   <insert id="insertContact"
>> parameterClass="contact">
>>     insert into CONTACT (PROP1, ...) values
>> (#property1#, ...)
>>     <selectKey resultClass="long" type="post">
>>       select @@IDENTITY
>>     </selectKey>
>>   </insert>
>>
>> "@@IDENTITY" is the Sybase way to retrieve the
>> auto-incremented value of
>> the primary key column.
>>
>> As you can see: There is no mapping of the resulting
>> key to a property
>> of the parameter class. I'm retrieving the
>> auto-incremented key by
>> executing
>>
>>   Long generatedKey = (Long)
>> sqlMapClient.insert("insertContact", contact)
>>
>> Hope this will guide you to a working solution.

Mime
View raw message