ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Larry Meadors <larry.mead...@gmail.com>
Subject Re: selectkey in MYSQL
Date Sun, 23 Jan 2005 15:15:58 GMT
put your selectKey after the insert for MySQL.


On Sun, 23 Jan 2005 16:06:45 +0100, Balazs Fejes <fbalazs@gmail.com> wrote:
> Hi,
> 
> I am trying to build an application using iBatis, and MySQL.
> When I'm using an insert statement, and the ID of the row is generated
> using the autoincrement feature of mysql, for some reason <selectkey>
> still sets the attribute of the object (and returns with) 0.
> 
> Here's the insert statement:
>   <insert id="insertShow" parameterClass="show">
>       <selectKey resultClass="int" keyProperty="id">
>         SELECT LAST_INSERT_ID()
>       </selectKey>
>     insert into shows ( title, coverimage )
>     values ( #title#, #coverimage# )
>   </insert>
> 
> The insert succeeds, the ID is properly generated for the row.
> If I execute the same 2 statements (insert and then select
> last_insert_id()) on the mysql console, it does return the proper last
> generated ID.
> But executing this test code:
>     public void testInsertShow() {
>         Show atlantis = new Show("SG Atlantis");
>         int key = showsDao.insertShow(atlantis);
>         System.out.println("id ="+atlantis.getId());
>     }
> 
> Shows that the Id attribute is set to 0. Also, the return value (key) is 0.
> I did a search on the mailing list, and I haven't found any previous
> record of this mysql feature yet, did anyone succeed to get this
> working?
> 
> --
> Best regards,
> Balazs
>

Mime
View raw message