Doubt with mybatis-generator and generatedKey

303 views
Skip to first unread message

Javier Domingo

unread,
Apr 23, 2012, 7:48:59 PM4/23/12
to mybati...@googlegroups.com
Hi,

I have been looking at the docs of mybatis-generator etc. And I have tried to figure out how it works, or at least, how do I need it to work. Finally, after reading several times the docs, I need a confirmation about if what the conclusions I got are good or not.

The idea is that I can specify a database connection, and it will take all the info, etc. to be able to access through the generated code the database.

If I want to insert a new object in a table, and I want it to be with the autoincrement feature of the DB system itself, I have to declare generatedKey tag inside that table declaration. In all inserts I do through that classes/Mappers, I will get specified fields auto_generated.

And now the questions come:
  • If I do something like this:
    <table tableName="%">
      <generatedKey column="id" sqlStatement="MySql"/>
    </table>

I will get all the tables in the database, and
a) all the fields named id that have the auto_increment flag, will be configured to take that value when inserting.
b) all the fields named id, will be configured to take the value through the auto_increment's value.

  • Is there any way to make the generator use auto_increment feature in all the fields that have it available? Something like the config below to do that:
    <table tableName="%">
      <generatedKey sqlStatement="MySql"/>
    </table>

  • In the documentation says it will set the generatedKey element to the correct value (through the sqlStatement that applies to the DB). I suppose it will do that in all the insert statements (all of them). In case I don't specify the <generatedKey> option in the generator, it will build the insert statements with the not-null fields. So if I don't specify the id (for example) field, the sentence it will generate will be an insert that the DB will fill-up the id field with the next value?
I say this to try to understand how it internally works. And another way to use autoincremented keys without specifying them in the config file. However, I have realized that if what I said is true, the only bad thing about not using generatedKey option is that the inserted object will not have its id (for example) set up.

I Hope you understood my English,

Cheers,

Javier Domingo

Bogdan Tanase

unread,
Apr 24, 2012, 1:13:06 AM4/24/12
to mybati...@googlegroups.com
Hi Javier,

For autoincrement I use this:

<generatedKey column="id" sqlStatement="JDBC"/>

and the autoincremented value is received from the driver. Works fine with MySQL.

upon insert the generated id is assigned to the id property of your pojo. Example:

Order order = new Order();
// ... 
// before insert order.id is null
mapper.insert(order);

// at this point order.id has the generated autoincrement value

If you don't use generatedKey, inserts should work fine, but the id property of your pojo won't be populated on insert. It won't generate in your mapper insert tag useGeneratedKeys="true" attribute.

I hope it helps, I didn't had my coffee yet :)

Javier Domingo

unread,
Apr 24, 2012, 1:22:14 AM4/24/12
to mybati...@googlegroups.com


El 24/04/2012 07:13, "Bogdan Tanase" <bfta...@gmail.com> escribió:
>
> Hi Javier,
>
> For autoincrement I use this:
>
> <generatedKey column="id" sqlStatement="JDBC"/>

This seems much cleaner than the other option (which is more specific) and I suppose the JDBC can be doing stuff under the interface call to the function.


>
> and the autoincremented value is received from the driver. Works fine with MySQL.
>
> upon insert the generated id is assigned to the id property of your pojo. Example:
>
> Order order = new Order();
> // ... 
> // before insert order.id is null
> mapper.insert(order);
>
> // at this point order.id has the generated autoincrement value
>
> If you don't use generatedKey, inserts should work fine, but the id property of your pojo won't be populated on insert. It won't generate in your mapper insert tag useGeneratedKeys="true" attribute.
>
> I hope it helps, I didn't had my coffee yet :)
>

And about making all autoincrementable columns to use it?

Jeff Butler

unread,
Apr 24, 2012, 7:41:38 AM4/24/12
to mybati...@googlegroups.com
The generator will not automatically discern auto increment columns.
If you do something like this:

<table name="%">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>

then the generator will assume that ALL tables have an "id" column
that is auto increment. If that is your situation, then it will work.
Otherwise you will need to specify the generatedKey value correctly
for each table.

Jeff Butler

Javier Domingo

unread,
Apr 24, 2012, 7:59:22 AM4/24/12
to mybati...@googlegroups.com
Well, I designed the database so that all the fields named id had auto_increment, so mybatis-generator will print out a warning saying that the column doesn't exist.

And it is a shame that there is no option to specify "Use all the auto increments columns you can"

Thank you!
Javier Domingo


2012/4/24 Jeff Butler <jeffg...@gmail.com>
Reply all
Reply to author
Forward
0 new messages