Skip to content
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

sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class #38

Open
helloworldtang opened this issue Apr 27, 2017 · 7 comments

Comments

@helloworldtang
Copy link

遇到了奇怪的报错:

Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
at tk.mybatis.mapper.mapperhelper.MapperHelper.setSqlSource(MapperHelper.java:247)
at tk.mybatis.mapper.mapperhelper.MapperHelper.processConfiguration(MapperHelper.java:310)
at tk.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:54)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 52 more
Caused by: java.lang.RuntimeException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
at tk.mybatis.mapper.mapperhelper.MapperTemplate.setSqlSource(MapperTemplate.java:246)
at tk.mybatis.mapper.mapperhelper.MapperHelper.setSqlSource(MapperHelper.java:244)
... 57 more
Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
at tk.mybatis.mapper.mapperhelper.MapperTemplate.getEntityClass(MapperTemplate.java:278)
at tk.mybatis.mapper.provider.base.BaseUpdateProvider.updateByPrimaryKeySelective(BaseUpdateProvider.java:64)
at sun.reflect.GeneratedMethodAccessor61.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at tk.mybatis.mapper.mapperhelper.MapperTemplate.setSqlSource(MapperTemplate.java:234)
... 58 more

@abel533
Copy link
Owner

abel533 commented Apr 27, 2017

应该是你写错了。有可能属于下面的某种情况:

AMapper<A> extends Mapper<A>

或者

AMapper extends Mapper

正确的应该是

AMapper extends Mapper<A>

@helloworldtang
Copy link
Author

刚才把代码搜了一遍,写法和上面说的一致。

我这边的场景是这样的:

这种写法就不报错
package com.sns.schedule;
@SpringBootApplication(scanBasePackages = {"com.sns"})
public class Application{

这样写就报上面的错(之所以这么折腾老代码,是觉得下面的写法优雅):
package com.sns;
@SpringBootApplication
public class Application{

@abel533
Copy link
Owner

abel533 commented Apr 28, 2017

还有一种可能就是你扫描到基类接口了。这类接口没有指定泛型,不能被扫描。

@helloworldtang
Copy link
Author

helloworldtang commented May 1, 2017

老代码比较早,当时还没有通用mapper的autoconfig,

通用mapper的的MyMapper.java在“com.sns.data”下,
业务Mapper是使用“tk.mybatis.spring.mapper.MapperScannerConfigurer”来自动配置,

你看看有没有上面写的问题,具体代码如下:
mapperscanner

@abel533
Copy link
Owner

abel533 commented May 1, 2017

你用 mybatis-starter 了吗?也有可能是自动配置扫描到了接口。

@helloworldtang
Copy link
Author

这个有。代码写的比较乱,把能用或不能用的dependencies都放到Pom文件中了。
但是在代码中显式声明了tk.mybatis.spring.mapper.MapperScannerConfigurer,按照starter这种自动配置的逻辑,如果有MapperScannerConfigurer的实例了,就不会再重复初始化实例了
这样看的话,应该也没有什么影响吧

@JohnNiang
Copy link

JohnNiang commented Oct 27, 2017

这里有一个问题,如果自定义的Mapper接口在AMapper所在包的下面或者子包下面也会报这样的错误。
@abel533

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants