Skip to content

There is case that fail to register type alias when exists anonymous class since mybatis-spring 2.0.1 #362

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
ifrit2000 opened this issue Apr 11, 2019 · 4 comments · Fixed by #363
Assignees
Labels
Milestone

Comments

@ifrit2000
Copy link

version
mybatis-spring 2.0.1
mybatis 3.5.1

It will throw org.apache.ibatis.type.TypeException , when exist two SQL provider.

In class org.mybatis.spring.SqlSessionFactoryBean , line 464, it invoke registerAlias(Class<?> type) directly, skip registerAliases(String packageName, Class<?> superType) . In old version ,registerAlias(Class<?> type) was invoke by registerAliases(String packageName, Class<?> superType), will skip some classes.

TypeAliasRegistry

  public void registerAliases(String packageName, Class<?> superType) {
    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
    resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
    Set<Class<? extends Class<?>>> typeSet = resolverUtil.getClasses();
    for (Class<?> type : typeSet) {
      // Ignore inner classes and interfaces (including package-info.java)
      // Skip also inner classes. See issue #6
      if (!type.isAnonymousClass() && !type.isInterface() && !type.isMemberClass()) {
        registerAlias(type);
      }
    }
  }

  public void registerAlias(Class<?> type) {
    String alias = type.getSimpleName();
    Alias aliasAnnotation = type.getAnnotation(Alias.class);
    if (aliasAnnotation != null) {
      alias = aliasAnnotation.value();
    }
    registerAlias(alias, type);
  }

config

mybatis:
  type-aliases-package: io.github.cd871127

Mapper

@Mapper
public interface TestMapper {

    @SelectProvider(type = TestProvider.class, method = "test")
    String test();

    class TestProvider {
        public String test() {
            return new SQL() {{
                SELECT("a");
                FROM("test");
            }}.toString();
        }

    }
}

@Mapper
public interface Test2Mapper {

    @SelectProvider(type = Test2Provider.class, method = "test")
    String test();

    class Test2Provider {
        public String test() {
            return new SQL() {{
                SELECT("a");
                FROM("test");
            }}.toString();
        }

    }
}

Exctption

Caused by: org.apache.ibatis.type.TypeException: The alias '' is already mapped to the value 'io.github.cd871127.hodgepodge.cloud.t66y.mapper.TestMapper$TestProvider$1'.
	at org.apache.ibatis.type.TypeAliasRegistry.registerAlias(TypeAliasRegistry.java:157)
	at org.apache.ibatis.type.TypeAliasRegistry.registerAlias(TypeAliasRegistry.java:147)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:464)
	at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:424)
	at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:554)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.sqlSessionFactory(MybatisAutoConfiguration.java:150)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$fe1ce91f.CGLIB$sqlSessionFactory$0(<generated>)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$fe1ce91f$$FastClassBySpringCGLIB$$6ab7cac8.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$fe1ce91f.sqlSessionFactory(<generated>)
	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:154)
	... 64 common frames omitted

@kazuki43zoo kazuki43zoo changed the title Fail to registered type alias using mybatis-spring 2.0.1 Thare is case that fail to register type alias when exists sql provider class of inner type since mybatis-spring 2.0.1 Apr 11, 2019
@ksmith97
Copy link

This also has a similar effect if there are enums present in the package.

@kazuki43zoo kazuki43zoo changed the title Thare is case that fail to register type alias when exists sql provider class of inner type since mybatis-spring 2.0.1 Thare is case that fail to register type alias when exists anonymous class since mybatis-spring 2.0.1 Apr 12, 2019
@kazuki43zoo kazuki43zoo self-assigned this Apr 12, 2019
@kazuki43zoo kazuki43zoo added this to the 2.0.2 milestone Apr 12, 2019
kazuki43zoo added a commit to kazuki43zoo/spring that referenced this issue Apr 12, 2019
@kazuki43zoo
Copy link
Member

kazuki43zoo commented Apr 12, 2019

This bug make by #359. Should be filtered anonymous class, interface and inner class such same as TypeAliasRegistry#registerAliases.

Workaround:
There is possibility that can be prevent this issue by narrowing down the package.

e.g.

mybatis:
  type-aliases-package: io.github.cd871127.**.model

kazuki43zoo added a commit that referenced this issue Apr 12, 2019
Filter anonymous class and interface when package scan
@kazuki43zoo
Copy link
Member

kazuki43zoo commented Apr 12, 2019

Hi @cd871127, Thank you for your report!

I've fixed this and 2.0.2-SNAPSHOT has been deployed OSS snapshot repository . Could you check can be resolve this issue on your application using 2.0.2-SNAPSHOT? If there is problem in latest 2.0.2-SNPASHOT, please feedback.

Thanks again!

@kazuki43zoo kazuki43zoo changed the title Thare is case that fail to register type alias when exists anonymous class since mybatis-spring 2.0.1 There is case that fail to register type alias when exists anonymous class since mybatis-spring 2.0.1 Apr 13, 2019
@ifrit2000
Copy link
Author

@kazuki43zoo it's ok using 2.0.0, thanks a lot!

pulllock pushed a commit to pulllock/mybatis-spring that referenced this issue Oct 19, 2023
pulllock pushed a commit to pulllock/mybatis-spring that referenced this issue Oct 19, 2023
Filter anonymous class and interface when package scan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants