我认为你应该在第二个项目/模块中添加你的实体的@EntityScan注释包
所以我没有正确使用SpringBoot功能。以下是我遵循的步骤。请记住项目的架构:
- Parent maven project (my.package) |-Module Base (with IndexSetup class and [initialy] hibernate.cfg.xml in /resources. It also had in the beginning LocalDatabase class to access to the local db via hibernate) |-Module Indexing (that depends on Base) |-Module Server (that also depends on Base) |-Database file (myLocalDB)
1)首先我删除了 hibernate.cfg.xml 从Base中删除它并将其放入Indexing模块的资源中。我这样做是因为SpringBoot有自己的配置机制。我还从Base中删除了LocalDatabase类(因为SpringBoot不需要它),并且在索引模块中也删除它(确实使用它)。
hibernate.cfg.xml
2)关注[this]( https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html] 我补充道 spring-boot-starter-data-jpa 到服务器模块pom.xml。
spring-boot-starter-data-jpa
3)以下 本教程 我创建了一个JPA存储库 IndexSetupRepository 几乎只有一行代码:
IndexSetupRepository
public interface IndexSetupRepository extends CrudRepository<IndexSetup, Integer> {
}
4)在服务器中 application.properties 我添加了这些行:
application.properties
# Embedded database configuration # The embedded database file is one level above the Server folder (ie directly within the parent project) # we use the auto server mode to be able to use the database simultaneously in the indexer and the server spring.datasource.url=jdbc:h2:file:../myLocalDB;AUTO_SERVER=TRUE spring.datasource.username=myName # This parameter helped me discover that SpringBoot was not targetting the right table name. spring.jpa.hibernate.ddl-auto=validate
5)正如SpringBoot告诉我它找不到名为的表 index_setup (看到 骆驼案转换为_ ),我不得不将此行添加到application.properties:
index_setup
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
6)现在,当我得到“实体未管理”时,我最终补充道 @EntityScan 对许多人建议我这样做的服务器主类的注释。
@EntityScan
@EntityScan("my.package.Entities")
请注意 @EntityScan 应该指向包含实体类的文件夹而不是实体类本身即ie @EntityScan("my.package.Entities.IndexSetup") 不工作。
@EntityScan("my.package.Entities.IndexSetup")