java - spring boot 找不到实体

标签 java spring hibernate maven spring-boot

我正在运行具有以下结构的 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 时中断。

最佳答案

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

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

所有这些都应该包含在你的引导应用程序类中,如下所示

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

关于java - spring boot 找不到实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26780994/

相关文章:

java - 正则表达式并不总是使用尖括号

java - Spring未找到Repository注释类

java - @ManyToMany 映射不会更新相应的表

java - hibernate transient 成员的默认值

java - 使用创建时提到的值创建 ArrayList

java - Glassfish Derby 连接池,连接未返回到池中

Java 字符串哈希码缓存

java - Spring+hibernate 创建在类路径资源 [applicationContext.xml] 中定义的名称为 'dataSource' 的 bean 时出错

java - 使用 Axios 从 React 获取请求中的 Spring Pageable 对象

java - 我们可以在 hibernate.cfg.xml 文件中使用 hibernate.hbm2ddl.auto=create 和 hibernate.hbm2ddl.auto=update 吗