Skip to content

There is case that duplicate loading same mapper file when use on Spring Boot 1.4.x(1.4.2 or before) #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zxkane opened this issue Oct 26, 2016 · 8 comments
Assignees
Milestone

Comments

@zxkane
Copy link

zxkane commented Oct 26, 2016

Everything works fine when using mybatis-spring-boot-starter 1.1.1 and spring boot 1.3.8. Even upgrading mybatis lib to latest 3.4.1.

After upgrading spring boot to 1.4.1, I got below exception when launching my app,

Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'URL [jar:file:build/libs/hezhibo-backend-1.0.0.jar!/BOOT-INF/classes/com/cn/zbb/mapping/DmClassInfoMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.cn.zbb.dao.DmClassInfoDao.mapper_resultMap[BaseResultMap]_collection[teacher]
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:522)
at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:381)
at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:546)
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.sqlSessionFactory(MybatisAutoConfiguration.java:130)
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$52861ccb.CGLIB$sqlSessionFactory$0()
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$52861ccb$$FastClassBySpringCGLIB$$821313a.invoke()
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$52861ccb.sqlSessionFactory()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 67 common frames omitted
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.cn.zbb.dao.DmClassInfoDao.mapper_resultMap[BaseResultMap]_collection[teacher]
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:120)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:92)
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:520)
... 80 common frames omitted
Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.cn.zbb.dao.DmClassInfoDao.mapper_resultMap[BaseResultMap]_collection[teacher]
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:837)
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:809)
at org.apache.ibatis.session.Configuration.addResultMap(Configuration.java:591)
at org.apache.ibatis.builder.MapperBuilderAssistant.addResultMap(MapperBuilderAssistant.java:214)

The mapper xml looks like below,

<mapper namespace="com.cn.zbb.dao.DmClassInfoDao" >
  <resultMap id="BaseResultMap" type="com.cn.zbb.model.DmClassInfo" >
    <id column="DMCI_ID" property="dmciId" jdbcType="INTEGER" />
    <result column="DMCI_Class_Name" property="dmciClassName" jdbcType="VARCHAR" />
    <result column="DMCI_Teacher_ID" property="dmciTeacherId" jdbcType="INTEGER" />
    <result column="DMCI_Remark" property="dmciRemark" jdbcType="VARCHAR" />
    <result column="DMCI_School_ID" property="dmciSchoolId" jdbcType="INTEGER" />
    <result column="DMCI_Grade_Name" property="dmciGradeName" jdbcType="VARCHAR" />
    <result column="DMCI_Grade_Type" property="dmciGradeType" jdbcType="VARCHAR" />
    <result column="DMCI_Update_Time" property="dmciUpdateTime" jdbcType="TIMESTAMP" />
    <result column="DMCI_GUID" property="dmciGuid" jdbcType="VARCHAR" />
    <collection property="teacher" ofType="com.cn.zbb.model.DmTeacherInfo">
        <id column="DMTI_ID" property="dmtiId" jdbcType="INTEGER" />
        <result column="DMTI_Teacher_Name" property="dmtiTeacherName" jdbcType="VARCHAR" />
        <result column="DMTI_Sex" property="dmtiSex" jdbcType="VARCHAR" />
        <result column="DMTI_ID_Number" property="dmtiIdNumber" jdbcType="VARCHAR" />
        <result column="DMTI_Home_Address" property="dmtiHomeAddress" jdbcType="VARCHAR" />
        <result column="DMTI_Telephone" property="dmtiTelephone" jdbcType="VARCHAR" />
        <result column="DMTI_Mailbox" property="dmtiMailbox" jdbcType="VARCHAR" />
        <result column="DMTI_Education" property="dmtiEducation" jdbcType="VARCHAR" />
        <result column="DMTI_QQ" property="dmtiQq" jdbcType="VARCHAR" />
        <result column="DMTI_State" property="dmtiState" jdbcType="VARCHAR" />
        <result column="DMTI_School_ID" property="dmtiSchoolId" jdbcType="INTEGER" />
        <result column="DMTI_Update_Time" property="dmtiUpdateTime" jdbcType="TIMESTAMP" />
        <result column="DMTI_GUID" property="dmtiGuid" jdbcType="VARCHAR" />
    </collection>
  </resultMap>

Any suggestion?

@kazuki43zoo
Copy link
Member

Hi @zxkane, can you provide a repro project on GitHub ? I want to confirm configurations in your application.

Thanks.

@zxkane
Copy link
Author

zxkane commented Oct 26, 2016

Thanks for quick response.

You can play it with the test repo(
https://github.com/zxkane/mybatis-spring-boot-test).

I also observe something else strange. The app can be launched well in
IDE(IntelliJ IDEA). But it would fail to be launched after packing it as
fat jar.

./gradlew clean build
java -jar build/libs/mybatis-spring-boot-test-1.0.0.jar

Kane

https://vme360.com

On Wed, Oct 26, 2016 at 10:55 AM, Kazuki Shimizu [email protected]
wrote:

Hi @zxkane https://github.com/zxkane, can you provide a repro project
on GitHub ? I want to confirm configurations in your application.

Thanks.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#108 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAzeJwqK199O1nGkm-vkePeJ73kzJh0Rks5q3sE7gaJpZM4Kgs3t
.

@kazuki43zoo
Copy link
Member

kazuki43zoo commented Oct 26, 2016

Recommendation (=workaround)

Hi @zxkane ,
Probably, this issue is problem of the Spring Boot 1.4 (or/and Spring Framework 4.3). I found a related issue on Spring Framework JIRA ticket (SPR-13685) .

I will suggest to use mapper file auto loading feature instead of. (this solution is recommended)

  • delete the mybatis.mapperLocations property.

    mybatis:
      typeAliasesPackage: com.cn.zbb.model
    -  mapperLocations: classpath*:**/mapper/*.xml
  • change storing directory and file name of mapper file as follow:

    .
    └── src
        └── main
            ├── java
            │   └── com
            │       └── cn
            │           └── zbb
            │               ├── MybatisSpringBootApp.java
            │               ├── controller
            │               ├── dao
            │               │   ├── DmClassInfoDao.java
            │               │   └── DmTeacherInfoDao.java
            │               ├── model
            │               └── service
            └── resources
                └── com
                    └── cn
                        └── zbb
    -                        └── mapper
    -                            ├── DmClassInfoMapper.xml
    -                            └── DmTeacherInfoMapper.xml
    +                        └── dao
    +                            ├── DmClassInfoDao.xml
    +                            └── DmTeacherInfoDao.xml

And you can use the @Mapper instead of the @MapperScan.

  • Add the @org.apache.ibatis.annotations.Mapper interface on mapper interfaces as follow:

    + @Mapper
    public interface DmClassInfoDao {
        // ...
    }
  • Delete the @MapperScan from java config

    @SpringBootApplication
    @ComponentScan(basePackages = {"com.cn.zbb"})
    - @MapperScan("com.cn.zbb.dao")
    public class MybatisSpringBootApp {
        // ...
    }

Thanks.

@zxkane
Copy link
Author

zxkane commented Oct 27, 2016

Kazuki, thank you for having look at this issue deeply.

You recommendation is really good. After making those changes, the project
can be launched with spring boot 1.4.1.

Looks like something is broken in spring mvc, pls free to close this issue.

Kane

https://vme360.com

On Thu, Oct 27, 2016 at 5:06 AM, Kazuki Shimizu [email protected]
wrote:

Hi @zxkane https://github.com/zxkane ,
Probably, this issue is problem of the Spring Boot 1.4 (or/and Spring
Framework 4.3). I found a related issue on Spring Framework JIRA ticket (
SPR-13685 https://jira.spring.io/browse/SPR-13685) .

I will suggest to use mapper file auto loading feature instead of. (this
solution is recommended)

delete the mapperLocations property.

mybatis:
typeAliasesPackage: com.cn.zbb.model- mapperLocations: classpath_:__/dao/_.xml

change storing directory and file name of mapper file as follow:

.
└── src
└── main
├── java
│ └── com
│ └── cn
│ └── zbb
│ ├── MybatisSpringBootApp.java
│ ├── controller
│ ├── dao
│ │ ├── DmClassInfoDao.java
│ │ └── DmTeacherInfoDao.java
│ ├── model
│ └── service
└── resources
└── com
└── cn
└── zbb- └── mapper+ └── dao- ├── DmClassInfoMapper.xml+ ├── DmClassInfoDao.xml- └── DmTeacherInfoMapper.xml+ └── DmTeacherInfoDao.xml

And you can use the @Mapper instead of the @MapperScan.

Add the @org.apache.ibatis.annotations.Mapper interface on mapper
interfaces as follow:

  • @Mapper
    public interface DmClassInfoDao {
    // ...
    }

    Delete the @MapperScan from java config

    @SpringBootApplication
    @componentscan(basePackages = {"com.cn.zbb"})- @MapperScan("com.cn.zbb.dao")
    public class MybatisSpringBootApp {
    // ...
    }

Thanks.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#108 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAzeJ3Qqgf09duYRktwcyw0qU8-3Llcuks5q38DIgaJpZM4Kgs3t
.

@kazuki43zoo
Copy link
Member

kazuki43zoo commented Nov 22, 2016

Hi guys, there are bug that provides duplicate resources from the PathMatchingResourcePatternResolver since Spring Boot 1.4(Spring 4.3). For example, path pattern such as classpath*:/**/mappers/**/*.Mapper.xml does not work on Spring Boot 1.4.
This is a regression introduced by SPR-13685. For details refer to the SPR-14934.

Probably, this issue will fix since Spring Boot 1.4.3(Spring 4.3.5).

@kazuki43zoo
Copy link
Member

@kazuki43zoo
Copy link
Member

spring-projects/spring-boot#7449 has been resolved. (However, it has not been release yet)

@kazuki43zoo kazuki43zoo added bug and removed question labels Dec 1, 2016
@kazuki43zoo kazuki43zoo reopened this Dec 1, 2016
@kazuki43zoo
Copy link
Member

kazuki43zoo commented Dec 1, 2016

I will update to Spring Boot 1.4.3.RELEASE(+Spring 4.3.5).

@kazuki43zoo kazuki43zoo self-assigned this Dec 1, 2016
@kazuki43zoo kazuki43zoo added this to the 1.2.0 milestone Dec 1, 2016
@kazuki43zoo kazuki43zoo changed the title Compatibility issue with spring boot 1.4.x There is case that duplicate loading same mapper file when use on Spring Boot 1.4.2 or before Jan 6, 2017
@kazuki43zoo kazuki43zoo changed the title There is case that duplicate loading same mapper file when use on Spring Boot 1.4.2 or before There is case that duplicate loading same mapper file when use on Spring Boot 1.4.x(1.4.2 or before) Jan 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants