Skip to content

Fully executable Jar does not scan typeAlias #38

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
kazuki43zoo opened this issue Mar 28, 2016 · 19 comments
Closed

Fully executable Jar does not scan typeAlias #38

kazuki43zoo opened this issue Mar 28, 2016 · 19 comments
Assignees
Labels
Milestone

Comments

@kazuki43zoo
Copy link
Member

mybatis-spring-boot 1.0.1 does not scan typeAlias from specified package as follow:

mybatis.type-aliases-package=com.example.model

Specify a type alias in Mapper XML

<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.MessageMapper">
    <select id="findOne" resultType="Message"> <!-- ## Use type alias -->
        SELECT code, message FROM t_message WHERE code = #{code}
    </select>
</mapper>

Settings for building a fully executable jar

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable> <!-- Add -->
            </configuration>
        </plugin>
    </plugins>
</build>

Build a fully executable jar

$ ./mvnw clean install

Run a fully executable jar

$ java -jar target/xxxx.jar
...
        at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:118)
        ... 44 more
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Message'.  Cause: java.lang.ClassNotFoundException: Cannot find class: Message
        at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
        at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149)
        at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116)
        ... 48 more
...

Related issue : #35

@kazuki43zoo
Copy link
Member Author

I've added repro project for this.

https://github.com/kazuki43zoo/mybatis-spring-boot-gh-38

$ git clone https://github.com/kazuki43zoo/mybatis-spring-boot-gh-38.git
$ cd mybatis-spring-boot-gh-38
$ ./mvnw install
$ java -jar target/mybatis-spring-boot-gh-38-1.0.0-SNAPSHOT.jar
...
        at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:118)
        ... 44 more
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Message'.  Cause: java.lang.ClassNotFoundException: Cannot find class: Message
        at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
        at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149)
        at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116)
        ... 48 more
...

@kazuki43zoo
Copy link
Member Author

I've confirmed to fix this issue using mybatis-spring 1.2.5-SNAPSHOT.

How to install the mybatis-spring 1.2.5-SNAPSHOT into local repository.

$ git clone https://github.com/mybatis/spring.git
$ cd spring
$ git checkout 1.2.x
$ mvn install

And i changed a version of mybatis-spring.

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.5-SNAPSHOT</version>
</dependency>

Build a fully executable jar

$ ./mvnw clean install

Run a fully executable jar

$ java -jar target/mybatis-spring-boot-gh-38-1.0.0-SNAPSHOT.jar
...
2016-03-29 02:15:06.045  INFO 50107 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-03-29 02:15:06.107  INFO 50107 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-03-29 02:15:06.110  INFO 50107 --- [           main] com.example.Application                  : Started Application in 2.711 seconds (JVM running for 3.05)

Access to http://localhost:8080/

$ curl http://localhost:8080/
Hello!!

@kazuki43zoo
Copy link
Member Author

This issue will fix via mybatis/spring#121.

@emacarron
Copy link
Member

Thank you @kazuki43zoo !!

@eddumelendez @kazuki43zoo Just let us know if we need a new release of the spring module to fix this.

@kazuki43zoo
Copy link
Member Author

@eddumelendez , @eddumelendez

I think that spring-mybatis 1.2.5 and mybatis-spring-boot 1.0.2 should release as soon as possible.

@eddumelendez
Copy link
Member

Thanks @kazuki43zoo to report this issue

@emacarron I have tested version 1.2.5-SNAPSHOT and it is working fine. Do you think we can perform a release of mybatis-spring? Thanks in advance.

@emacarron
Copy link
Member

Of course we can. Let me know if spring-boot is ready for a release and will make both releases asap.

@emacarron emacarron modified the milestone: 1.0.2 Mar 29, 2016
@emacarron
Copy link
Member

Ah, sorry, I see there are no changes in spring-boot so we only need a release of the spring module. Lets go then.

@emacarron
Copy link
Member

Finally fixed by upgrading the MyBatis-Spring dependency with commit 8fe17c4

@gituser786
Copy link

gituser786 commented Sep 16, 2017

I am using spring boot version 1.5.4 along with spring-mybatis 1.3.0. mybatis-spring-boot-starter 1.3.0
I am getting same error when I execute as a jar file. But in eclipse I don't get any error. Please help to solve this issue

@kazuki43zoo
Copy link
Member Author

kazuki43zoo commented Sep 17, 2017

@gituser786 Could you provide a small reproduce project via GitHub repository?
I will guess that your application not used SpringBootVFS class.

@TracyBin
Copy link

TracyBin commented Nov 3, 2017

I encountered the same problem.There is no problem in eclipse but package executor jar

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.8.RELEASE</version>
	<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>
Caused by: java.lang.ClassNotFoundException: Cannot find class: UserMobileRlt
	at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.apache.ibatis.io.Resources.classForName(Resources.java:261) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:74) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:135) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:128) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:118) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:92) ~[mybatis-3.4.5.jar!/:3.4.5]
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:521) ~[mybatis-spring-1.3.1.jar!/:1.3.1]
	at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:380) ~[mybatis-spring-1.3.1.jar!/:1.3.1]
	at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:547) ~[mybatis-spring-1.3.1.jar!/:1.3.1]
	at com.asiainfo.config.MasterDataSourceConfig.masterSqlSessionFactory(MasterDataSourceConfig.java:66) ~[classes!/:0.0.1]
	at com.asiainfo.config.MasterDataSourceConfig$$EnhancerBySpringCGLIB$$900291c.CGLIB$masterSqlSessionFactory$0(<generated>) ~[classes!/:0.0.1]
	at com.asiainfo.config.MasterDataSourceConfig$$EnhancerBySpringCGLIB$$900291c$$FastClassBySpringCGLIB$$791687e5.invoke(<generated>) ~[classes!/:0.0.1]
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
	at com.asiainfo.config.MasterDataSourceConfig$
 @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MasterDataSourceConfig.MAPPER_LOCATION));
        sessionFactory.setTypeAliasesPackage(MasterDataSourceConfig.TYPEALIASESPACKAGE);
        return sessionFactory.getObject();
    }

@kazuki43zoo
Copy link
Member Author

Hi @TracyBin , please set the SpringBootVFS to an SqlSessionFactoryBean instance as follow:

SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);

@TracyBin
Copy link

TracyBin commented Nov 3, 2017

@kazuki43zoo , Yes,the problem is solved.Thanks for your reply.

@kazuki43zoo
Copy link
Member Author

Hi @TracyBin , I've added an explanation for this. See 5c77a8e.
Thanks.

@shreyasGit
Copy link

shreyasGit commented May 29, 2018

Thanks for the solution, its not working fully though for me.
i did set SpringBootVFS to an SqlSessionFactoryBean and was able to resolve Alias packages.
but now i am getting classnotfoundexception for typehandlers. it is not able to find type handlers.
i have set typehandlers using setTypeHandlersPackage method.
am i missing something? git stuck any help would be appreciated.

Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [com/sas/mkt/persistence/module/dbimpl/design/mybatis/mappers/AssociationMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'UUIDTypeHandler'.  Cause: java.lang.ClassNotFoundException: Cannot find class: UUIDTypeHandler
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:522)
	at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:381)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1688)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626)
	... 16 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'UUIDTypeHandler'.  Cause: java.lang.ClassNotFoundException: Cannot find class: UUIDTypeHandler
	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)
	... 19 more
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'UUIDTypeHandler'.  Cause: java.lang.ClassNotFoundException: Cannot find class: UUIDTypeHandler
	at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:118)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildResultMappingFromContext(XMLMapperBuilder.java:376)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:280)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:252)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:244)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:116)
	... 21 more
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'UUIDTypeHandler'.  Cause: java.lang.ClassNotFoundException: Cannot find class: UUIDTypeHandler
	at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
	at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149)
	at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116)
	... 26 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: UUIDTypeHandler
	at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
	at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
	at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
	at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116)
	... 28 more

@kazuki43zoo
Copy link
Member Author

@shreyasGit
Could you provide a reproduce project on GitHub?

@shreyasGit
Copy link

shreyasGit commented May 29, 2018 via email

@kazuki43zoo
Copy link
Member Author

kazuki43zoo commented May 29, 2018

If you need my help, I want to a small reproduce project(maven or gradle).
It is difficult to analyze issue with stack trace only in this case.

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.

6 participants