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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23366226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:41:13  来源:igfitidea点击:

Spring Boot w/ JPA: move @Entity to different package

springjpaspring-dataspring-data-jpaspring-boot

提问by Stefan K.

I'm having trouble with my first steps using Spring-Boot with JPA. I've started with a pretty minimalistic example from Gitusing Gradle.

我在使用带有 JPA 的 Spring-Boot 的第一步时遇到了问题。我使用 Gradle 的Git 中的一个非常简约的示例开始。

Now simply moving Customerto another package, let's say to hello2results in an exception Caused by: java.lang.IllegalArgumentException: Not an managed type: class hello2.Customer. I tried to add

现在简单地移动Customer到另一个包,让我们说 to hello2results in an exception Caused by: java.lang.IllegalArgumentException: Not an managed type: class hello2.Customer。我试着添加

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

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

to Application, but without success.

Application,但没有成功。

What am I doing wrong?

我究竟做错了什么?

回答by axtavt

Location of entities in Spring Boot can be configured using @EntityScan.

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

By default, @EnableAutoConfigurationenables entity scanning in the package where it's placed (if it's not a default package).

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

回答by tranductrinh

You must locate entities and repositories pakages by using

您必须使用以下命令定位实体和存储库包

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

@EntityScan(basePackages = "your.entities.pakage")

回答by bhaskar babu

this is what worked for me :

这对我有用:

@EnableJpaRepositories(basePackages ={ "package1","package2"})
@EntityScan(basePackages ={ "package3","package4"})

回答by Ashutosh S

Giving same package location (i.e base package) for below annotation worked for me :-

为以下注释提供相同的包位置(即基本包)对我有用:-

@SpringBootApplication(scanBasePackages = {"org.ashu.java.*"})
@EnableJpaRepositories(basePackages ={ "org.ashu.java.*"})    
@EntityScan(basePackages ={ "org.ashu.java.*"})