您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

SpringBoot“不是实体”

SpringBoot“不是实体”

碰巧我没有正确使用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")不起作用。

Java 2022/1/1 18:15:17 有481人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶