【问题标题】:spring boot not finding Entitiesspring boot 找不到实体
【发布时间】:2015-01-03 01:11:17
【问题描述】:

我正在运行具有以下结构的 Spring Boot 应用程序

Main Application 
 com/my/application/app/boot/AppStarter.java
 depends on Lib Application

Lib Application
 META-INF/package/persistence.xml
 com/my/application/data/Entity1.java

实体 1 是使用 @Entity@Table 注释的持久性对象

AppStarter如下:

@ComponentScan({ "com.my.application.sampleScan1",
        "com.my.application.sampleScan2"})
@EntityScan(basePackages={"com.my.application.data"})
@EnableAutoConfiguration
@Configuration
@PropertySources({
        @PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true),
        @PropertySource(value = "classpath:test.properties", ignoreResourceNotFound = true) })
@ImportResource({ "classpath:my/application/fake/fakeContext.xml"})
public class FakeAppBooter {

public static void main(String args[]) {
    SpringApplication.run(FakeAppBooter.class, args);
}
}

当我使用 spring boot 应用程序将其打包为 jar 时,Lib 应用程序位于 /lib/LibApplication.jar 内,但是当它尝试访问实体 Entity1 时,我得到一个:org.hibernate.MappingException: Unknown entity: com.my.application.data.Entity1

当它在 Eclipse 中执行时,它工作正常,它只会在我运行 spring-boot-plugin 生成的 jar 时中断。

【问题讨论】:

  • 同一条船。有桨吗?
  • 这已经很久了,我不记得实际的问题是什么。我记得的是 Spring boot 注释和 xml 配置不能很好地协同工作。如果您可以尝试切换到仅注释或仅 xml 配置,它可能会有所帮助。
  • 我错过了@EntityScan

标签: java spring hibernate maven spring-boot


【解决方案1】:

您需要对所有包进行组件扫描,并对所有实体进行实体扫描

com.uganda.** 和 com.uganda.**.entity

所有内容都应包含在您的引导应用程序类中,如下所示

 @SpringBootApplication
 @ComponentScan({ "com.uganda"})
 @EntityScan(basePackages= {"com.uganda"})
 public class MyBootpApplication {

【讨论】:

    【解决方案2】:

    最好不要单独使用注解,最好只使用

    @SpringBootApplication
    

    注释你的类,让 Spring Boot 完成剩下的工作。

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2019-03-18
      • 1970-01-01
      • 2020-07-04
      • 2015-07-10
      • 2020-07-31
      • 2020-11-11
      • 2017-10-11
      • 2021-03-14
      相关资源
      最近更新 更多