Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Doubt with mybatis-generator and generatedKey
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jeff Butler  
View profile  
 More options Apr 24 2012, 7:41 am
From: Jeff Butler <jeffgbut...@gmail.com>
Date: Tue, 24 Apr 2012 07:41:38 -0400
Local: Tues, Apr 24 2012 7:41 am
Subject: Re: Doubt with mybatis-generator and generatedKey
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 <javier...@gmail.com> wrote:

> El 24/04/2012 07:13, "Bogdan Tanase" <bftan...@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?

>> On Tue, Apr 24, 2012 at 2:48 AM, Javier Domingo <javier...@gmail.com>
>> 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.