碰巧我没有正确使用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不需要它),并将其也删除了在Indexing Module中(确实使用了它)。
2)在[this]([https://docs.spring.io/spring- boot/docs/current/reference/html/boot-features- sql.html]之后,](https://docs.spring.io/spring- boot/docs/current/reference/html/boot-features-sql.html])我添加spring-boot- starter-data-jpa
到服务器模块pom.xml。
3)在学习完本教程之后,我仅用IndexSetupRepository
一行代码创建了一个JPA存储库:
public interface IndexSetupRepository extends CrudRepository<IndexSetup, Integer> {
}
4)在服务器中,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中:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
6)现在,当我得到“实体不受管理”时,@EntityScan
正如许多人建议的那样,我最终在服务器主类中添加了注释。
@EntityScan("my.package.Entities")
请注意,@EntityScan
应指向包含实体类而不是实体类本身的文件夹,即该文件夹@EntityScan("my.package.Entities.IndexSetup")
不起作用。