ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeff Butler" <jeffgbut...@gmail.com>
Subject Re: select Key not working giving error.
Date Fri, 13 Jul 2007 13:38:58 GMT
Guys - @@identity and a sequence are two completely different things.

With @@identity, you get the key AFTER the record is inserted and it does
not need to be mapped - because iBATIS will return it to you as a result of
the insert() statement.

Sequences are different - you must get the new key BEFORE the record is
inserted.  The returned value needs to be stored somewhere.  You'll either
need to map it to a field in your object, or make the sequence call directly
in the insert statement as Sivaranjani described.

Jeff Butler

On 7/13/07, Christian Möller <dcmoeller@gmx.de> wrote:
>
> 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