带JPA的Spring Boot:将@Entity移动到不同的包

Ste*_* K. 29 spring jpa spring-data spring-data-jpa spring-boot

我在使用带JPA的Spring-Boot的第一步时遇到了麻烦.我从Git使用Gradle的一个非常简约的例子开始.

现在只需转移Customer到另一个包,让我们说hello2结果是异常Caused by: java.lang.IllegalArgumentException: Not an managed type: class hello2.Customer.我试着补充一下

@ComponentScan(basePackageClasses= {Customer.class}) // AND OR @EnableJpaRepositories(basePackageClasses= {Customer.class})

Application,但没有成功.

我究竟做错了什么?

axt*_*avt 41

可以使用配置Spring Boot中实体的位置@EntityScan.

默认情况下,@EnableAutoConfiguration在包中放置实体扫描(如果它不是默认包).


小智 31

您必须使用以查找实体和存储库包

@EnableJpaRepositories(basePackages = "your.repositories.pakage")

@EntityScan(basePackages = "your.entities.pakage")
Run Code Online (Sandbox Code Playgroud)


小智 6

这对我有用:

@EnableJpaRepositories(basePackages ={ "package1","package2"})
@EntityScan(basePackages ={ "package3","package4"})
Run Code Online (Sandbox Code Playgroud)