Spring Boot w/ JPA:将@Entity移动到不同的包。

时间:2022-09-11 16:44:07

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

我在使用JPA的spring引导时遇到了一些麻烦。我从Git使用Gradle的一个非常简单的例子开始。

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

现在,简单地将客户转移到另一个包中,让我们对hello2产生一个异常引起的异常:java.lang。非托管类型:类hello2.Customer。我试图添加

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

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

to Application, but without success.

应用,但没有成功。

What am I doing wrong?

我做错了什么?

4 个解决方案

#1


37  

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

可以使用@EntityScan配置Spring引导中实体的位置。

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

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

#2


24  

You must locate entities and repositories pakages by using

您必须通过使用来定位实体和存储库pakages。

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

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

#3


6  

this is what worked for me :

这就是我的工作:

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

#4


0  

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

提供相同的包位置(i。e基本包)以下注释为我工作:-。

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

#1


37  

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

可以使用@EntityScan配置Spring引导中实体的位置。

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

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

#2


24  

You must locate entities and repositories pakages by using

您必须通过使用来定位实体和存储库pakages。

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

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

#3


6  

this is what worked for me :

这就是我的工作:

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

#4


0  

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

提供相同的包位置(i。e基本包)以下注释为我工作:-。

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