mybatis / old-google-code-issues

Automatically exported from code.google.com/p/mybatis
2 stars 4 forks source link

MyBatis Generator: Mapper XML is missing keyColumn for inserts with generated key identity fields #670

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using? 3.1.1

Please describe the problem.  Unit tests are best!

A table description like the following that specifies a generated-key column 
should create a mapper that uses that column.

<table tableName="HOLDING" enableCountByExample="false" 
enableDeleteByExample="false" enableSelectByExample="false" 
enableUpdateByExample="false">
  <property name="rootInterface" value="CustomHoldingMapper"/>
  <property name="rootClass" value="AbstractHolding"/>
  <generatedKey column="HOLDING_ID" identity="true" sqlStatement="JDBC"/>
  <columnOverride column="ACC_ID" javaType="String" /> 

...

What is the expected output? 
  <insert id="insert" keyColumn="HOLDING_ID" keyProperty="holdingId" parameterType="Holding" useGeneratedKeys="true">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into HOLDING (ACC_ID, ... 

What do you see instead?
  <insert id="insert" keyProperty="holdingId" parameterType="Holding" useGeneratedKeys="true">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into HOLDING (ACC_ID, ...

Can you provide stack trace, logs, error messages that are displayed?

Please provide any additional information below.

This changes the behavior of generated keys.  In Oracle, the generated key is 
taken from ROWID instead of HOLDING_ID.  
The workaround is to hand-edit the files after they are generated, but that 
makes it difficult to automate the build.

Original issue reported on code.google.com by paulkrau...@alum.mit.edu on 14 Sep 2012 at 10:06

GoogleCodeExporter commented 9 years ago
This is the workaround I use in my POM.  I'm posting it here in case anyone 
needs it.

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>Post-process MyBatis Artifacts</id>
                        <phase>process-sources</phase>
                        <configuration>
                            <target name="process-mapper-xml" description="Fix the XML generated by MyBatis Generator">
                                <property name="daodir" location="src/gen/resources/com/mycompany/myproject/database/dao" />
                                <replace file="${daodir}/HoldingMapper.xml">
                                    <replacetoken><![CDATA[id="insert" keyProperty="holdingId"]]></replacetoken>
                                    <replacevalue><![CDATA[id="insert" keyColumn="HOLDING_ID" keyProperty="holdingId"]]></replacevalue>
                                </replace>
                                <replace file="${daodir}/HoldingMapper.xml">
                                    <replacetoken><![CDATA[id="insertSelective" keyProperty="holdingId"]]></replacetoken>
                                    <replacevalue><![CDATA[id="insertSelective" keyColumn="HOLDING_ID" keyProperty="holdingId"]]></replacevalue>
                                </replace>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

Original comment by paulkrau...@alum.mit.edu on 9 Oct 2012 at 3:06

GoogleCodeExporter commented 9 years ago

Original comment by eduardo.macarron on 13 Oct 2012 at 6:19

GoogleCodeExporter commented 9 years ago
See this discussion thread:

https://groups.google.com/forum/?fromgroups=#!topic/mybatis-user/6vQ77_rT2lg

This issue has morphed into several things:

1. Add keyColumn attribute to the generated insert statement
2. Add code in the generator to guard against an identity column being the last 
column in the table
3. Add the ability to specify multiple <generatedKey> elements for different 
databases

Original comment by jeffgbut...@gmail.com on 2 Nov 2012 at 2:17

GoogleCodeExporter commented 9 years ago
Also, add keyColumn to any generated @Options

Original comment by jeffgbut...@gmail.com on 10 Jan 2013 at 4:20