【问题标题】:What is spring boot's databse initialization behaviour with hibernate JPA?什么是休眠 JPA 的 Spring Boot 数据库初始化行为?
【发布时间】:2025-12-10 04:10:02
【问题描述】:
我正在使用 Spring in Action 第 5 版这本书来学习 Spring。它告诉我编写一些由 H2 数据库和 schema.sql data.sql 促进的 jdbc 代码。有效。然后它告诉我通过在 pom.xml 中包含 spring-boot-starter-data-jpa 包来切换到 JPA。但是我发现schema.sql和data.sql没有执行,因为数据库schema和我在schema.sql里写的不一样,里面没有数据。
我的印象是 hibernate 通过查看带有 @Entity 注释的类来为我创建表。它对我不起作用。我做了一些谷歌搜索并添加了一个属性设置来关闭创建表操作。但是,该模式与本书在上一节中的 schema.sql 中所写的不同。例如,有一个名为“createdAt”的 java 对象字段,在 schema.sql 中定义为“createdAt”(书上是这样写的),但 hibernate 期望的是“created_at”。
【问题讨论】:
标签:
spring
hibernate
spring-boot
jpa
【解决方案1】:
我们必须更改命名策略以防止createdAt 成为created_at
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
注意:spring.jpa.hibernate.naming.strategy 不是使用 Hibernate 5 实现的 Spring JPA 支持的属性。
对于 Spring Boot 1.4.x
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl