最近遇到一个问题:在生产中有两个模块,一个模块负责定时任务,并负责存库,另一个模块则负责读取数据库,并展示,由于是两个不同的模块,所以每个模块中都有一部分entity是公用的,导致整个系统的代码重复率偏高,所以决定将公共部分的entity提取出来,放到一个公共的模块当中,然后运行以前的代码,结果就抛了如下的异常:
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.chhliu.springbooth2entity.User
at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:70) ~[spring-data-jpa-1.10.6.RELEASE.jar:na]
大概问题就是entity没有扫描到,于是在主类中加了如下的注解:
@ComponentScan(basePackages="com.chhliu.springbooth2entity")
发现问题依然没有解决,于是在主类中再加上了如下注解:
@EnableJpaRepositories(basePackages={"com.chhliu.springbooth2entity"})
这个时候,就有点病急乱投医了,因为@EnableJpaRepositories的作用是用来扫描jpa的dao层的,而不是entity,所以就搜索了下spring boot怎么扫描entity,结果发现了@EntityScan注解,如是加上如下注解:
@EntityScan(basePackages={"com.chhliu.springbooth2entity"})
问题解决了。