Quantcast

Doubt with mybatis-generator and generatedKey

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Doubt with mybatis-generator and generatedKey

Javier Domingo
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Doubt with mybatis-generator and generatedKey

Bogdan Tanase
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 :)

On Tue, Apr 24, 2012 at 2:48 AM, Javier Domingo <[hidden email]> wrote:
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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Doubt with mybatis-generator and generatedKey

Javier Domingo


El 24/04/2012 07:13, "Bogdan Tanase" <[hidden email]> 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?
>
> On Tue, Apr 24, 2012 at 2:48 AM, Javier Domingo <[hidden email]> wrote:
>>
>> 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
>
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Doubt with mybatis-generator and generatedKey

Jeff Butler
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



On Tue, Apr 24, 2012 at 1:22 AM, Javier Domingo <[hidden email]> wrote:

>
> El 24/04/2012 07:13, "Bogdan Tanase" <[hidden email]> 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?
>
>
>>
>> On Tue, Apr 24, 2012 at 2:48 AM, Javier Domingo <[hidden email]>
>> wrote:
>>>
>>> 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
>>
>>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Doubt with mybatis-generator and generatedKey

Javier Domingo
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 <[hidden email]>
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



On Tue, Apr 24, 2012 at 1:22 AM, Javier Domingo <[hidden email]> wrote:
>
> El 24/04/2012 07:13, "Bogdan Tanase" <[hidden email]> 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?
>
>
>>
>> On Tue, Apr 24, 2012 at 2:48 AM, Javier Domingo <[hidden email]>
>> wrote:
>>>
>>> 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
>>
>>

Loading...