uuid generatedkeys

515 views
Skip to first unread message

Florian Gasc

unread,
Jun 17, 2015, 11:08:35 AM6/17/15
to mybati...@googlegroups.com
Hello,

I coundn't find the solution in mybatis-user related forum. So i posted my problem :

I have a problem with uuid, generatedKeys and mybatis.  keyColumn and keyProperty works as expected but not  for uuid type which always appear null after insert.

some peace of code :

        <insert id="insertTableUuid"
          useGeneratedKeys="true"
                                        keyColumn="attribute_uuid"
                                        keyProperty="tableuuid.uuid"
                      
        >
        insert into table_uuid values (default);
        </insert>

    public int insertTableUuid(@Param("tableuuid") TableUuid tableUuid);



    <resultMap type="fr.simplifia.storage.mapper.basic.resultmap.all.TableUuid"
        id="TableUuidResultMapAll">
        <id property="uuid"  typeHandler="UUIDTypeHandler" column="attribute_uuid" />
    </resultMap>


Any suggestion that may help me ? Thank's in advance for your help.

Filipe Sousa

unread,
Jun 18, 2015, 7:18:00 AM6/18/15
to mybati...@googlegroups.com
is the uuid generated by the database?

Florian Gasc

unread,
Jun 18, 2015, 7:52:21 AM6/18/15
to mybati...@googlegroups.com
Yes, it's auto generated by postgressql database. This function «uuid_generate_v1mc()» generate uuid upon insert (the generate value is the default for this attribute).

Florian Gasc

unread,
Jul 2, 2015, 3:36:26 AM7/2/15
to mybati...@googlegroups.com
Hello,

It's really impossible to retreive autogenerated uuid  ?

thanks in advance

Guy Rouillier

unread,
Jul 2, 2015, 6:43:56 PM7/2/15
to mybati...@googlegroups.com
I'm not sure I'm understanding your scenario.  Are you saying that you've defined your table such that the uuid column has a default value that invokes the uuid_generate_v1mc() function?  If you execute "insert into table_uuid values (default)" from pgAdmin, does the resulting row contain a generated UUID value?  Your resultMap isn't doing anything because you are doing an insert, not a select.  Finally, your keyProperty should be simply uuid and not tableuuid.uuid, since you are passing in TableUuid as your parameterType.  You should make that parameterType explicit.
 
--
Guy Rouillier
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Avast logo

This email has been checked for viruses by Avast antivirus software.
www.avast.com


Bassem Boubaker

unread,
Jul 9, 2015, 10:27:21 AM7/9/15
to mybati...@googlegroups.com
What is trying to say that upon insert, He should be able to get an instance of the object that was inserted including the field (typed as UUID in java) which was generated by default in the DataBase.The object is something like this:  

public class MyUuid {
private UUID   myUuidA;
private String name;
private int   id;

}

after insert, I get this object instanciated with the correct data inserted except the field  "private UUID   myUuidA".

myBatis XML file is something like this: 

<mapper namespace="fr.test.mybatis.UuidMapper">
<resultMap type="fr.test.mybatis.MyUuid" id="TableUuidResultMapAll">
<id property="id" column="id" />
<result property="myUuidA" typeHandler="fr.test.mybatis.UUIDTypeHandler" column="uuid" />
<result property="name" column="name" />
</resultMap>
<insert id="insertUuid" useGeneratedKeys="true" keyColumn="uuid,name,id"   keyProperty="myUuidA,name,id">
insert into table_uuid (uuid, name, id)values
(default,#{MyUuid.name},default);
</insert>
</mapper>


Any help?

Guy Rouillier

unread,
Jul 10, 2015, 12:53:24 AM7/10/15
to mybati...@googlegroups.com
Database, using example I found here to avoid having to install extensions: http://stackoverflow.com/questions/12505158/generating-a-uuid-in-postgres-for-insert-statement:
 
create table uuid_test
 (
 id uuid DEFAULT uuid_in((md5((random())::text))::cstring) not null primary key,
 name varchar(64) not null
 )
 
Mapper:
    <insert id="insertUuid" parameterType="org.sample.beans.UuidTest" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
  insert into uuid_test (
   name
  ) values (
   #{name}
  )
 </insert>
UuidTest.java:
 
package org.sample.beans;
 
public class UuidTest
   {
   private String id;
   private String name;
 
   public UuidTest()
      {
      super();
      }
 
   public String getId()
      {
         return id;
      }
 
   public void setId(String id)
      {
         this.id = id;
      }
 
   public String getName()
      {
      return name;
      }
 
   public void setName(String name)
      {
      this.name = name;
      }
   }
 
Test code:
 
         UuidTest uuidTest = new UuidTest();
         uuidTest.setName("MyBatis insert");
         int iRows = mapper.insertUuid(uuidTest);
         log.info("insertUuid rows inserted: " + iRows + " uuid: " + uuidTest.getId());
Result:
 
Main: insertUuid rows inserted: 1 uuid: 6a8025fa-0eee-0731-e299-a765d97b0ee2
--
Guy Rouillier
 
 
 
------ Original Message ------
From: "Bassem Boubaker" <boubaker.b...@gmail.com>
Sent: 7/9/2015 7:45:09 AM
Subject: Re: uuid generatedkeys
 
What is trying to say that upon insert, He should be able to get an instance of the object that was inserted including the field (typed as UUID in java) which was generated by default in the DataBase.The object is something like this:  

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Florian Gasc

unread,
Jul 15, 2015, 11:10:16 AM7/15/15
to mybati...@googlegroups.com, guy.ro...@gmail.com
Hi,

Thanks for answers. I tried your solution. But i would like to use uuid type.  As jdbc returns uuid, but mybatis not,  i think the problem is into type handler (http://www.manniwood.com/mybatis_stuff/index.html).

I will post my feedback when i found time for deeplier testing.


Thank's again. Bye.




Reply all
Reply to author
Forward
0 new messages