Java 在启动时执行DDL验证时,Hibernate不使用@Table。使用Flyway和TestContainers
我对Hibernate有问题。 我正在通过Flyway迁移创建表。 下面显示了初始迁移脚本的一部分片段。 脚本确实在运行(我可以在调试模式下看到它) 在脚本运行之后,Hibernate的验证器似乎没有使用我通过Java 在启动时执行DDL验证时,Hibernate不使用@Table。使用Flyway和TestContainers,java,spring,spring-boot,hibernate,flyway,Java,Spring,Spring Boot,Hibernate,Flyway,我对Hibernate有问题。 我正在通过Flyway迁移创建表。 下面显示了初始迁移脚本的一部分片段。 脚本确实在运行(我可以在调试模式下看到它) 在脚本运行之后,Hibernate的验证器似乎没有使用我通过javax.persistence提供的实体的表名 以下是为清晰起见略去部分内容的实体: 导入javax.persistence.*; @实体 @表(name=“IngreditCategories”) 公共类IngCreditCategory实现了有效性{ @身份证 @Generate
javax.persistence
提供的实体的表名
以下是为清晰起见略去部分内容的实体:
导入javax.persistence.*;
@实体
@表(name=“IngreditCategories”)
公共类IngCreditCategory实现了有效性{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@列(nullable=false,长度=128)
私有字符串名称;
...
}
创建表“IngCreditCategories”(
`id`bigint NOT NULL自动增量,
`description`varchar(255)默认为空,
`name`varchar(128)不为空,
主键(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=2默认字符集=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
查看日志,运行Flyway迁移,然后我得到hibernate验证错误
2020-10-24 11:22:11.952 DEBUG 91583 --- [ main] o.f.core.internal.command.DbMigrate : Successfully completed migration of schema `test` to version 1 - init
2020-10-24 11:22:11.961 DEBUG 91583 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Schema History table `test`.`flyway_schema_history` successfully updated to reflect changes
2020-10-24 11:22:11.970 INFO 91583 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 1 migration to schema `test` (execution time 00:00.107s)
2020-10-24 11:22:11.972 DEBUG 91583 --- [ main] org.flywaydb.core.Flyway : Memory usage: 82 of 354M
2020-10-24 11:22:12.088 INFO 91583 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-10-24 11:22:12.125 INFO 91583 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.20.Final
2020-10-24 11:22:12.237 INFO 91583 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-10-24 11:22:12.350 INFO 91583 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-10-24 11:22:12.998 WARN 91583 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [ingredient_categories]
2020-10-24 11:22:12.998 INFO 91583 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-10-24 11:22:13.420 INFO 91583 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2020-10-24 11:22:13.428 INFO 91583 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-10-24 11:22:13.434 ERROR 91583 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [ingredient_categories]
我无法决定这是一个休眠问题、飞行路线问题还是测试问题
这只是一个“启动”应用程序的测试:
@SpringBootTest
@测试容器
类DataApplicationTests{
@试验
void contextLoads(){
}
}
我环顾四周,发现很多人在Spring2X之前都遇到了一个问题,在flyway生成模式之前对表进行了验证……但这似乎已经“修复”,默认情况是flyway迁移在之前运行
我认为问题所在的行显示Hibernate希望表名为
模式验证:缺少表[成分类别]
因此,在运行和构建上下文/bean时,它似乎没有使用javax.constant.Table@Table(name=“IngreditCategories”)
注释
应用程序属性不太令人兴奋…我正在使用TestContainers:
#=========INTEGRATION TESTS========#
## Using TestContainers
spring.datasource.url=jdbc:tc:mysql:8.0.22:///
# Validate Schema
spring.jpa.hibernate.ddl-auto = validate
logging.level.org.flywaydb=DEBUG
从官方
默认情况下,Spring Boot将物理命名策略配置为
SpringPhysicalNamingStrategy。此实现提供了相同的功能
表结构为Hibernate 4:所有点都替换为下划线
而驼峰式大小写也被下划线所取代。默认情况下,所有
表名以小写形式生成,但可以
如果架构需要,请覆盖该标志
例如,电话号码实体映射到
电话号码表
因此,InCreditCategories
变成了Component\u categories
。对于数据库表名,使用snake case的常规约定。您可以在snake case中创建名称为的表
CREATE TABLE `ingredient_categories`
或者,如果您更喜欢使用Hibernate 5的默认设置,请设置以下属性:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
然后,您的表名仍保留在
InCreditCategories
中,如@table
注释中所示。有关的详细信息,谢谢。我可能会转回去做蛇案。我过去一直使用hibernate,让它设置表和列的名称。我会坚持这个惯例,但很高兴知道你可以改变它:)。进一步说,这听起来可能很愚蠢,但如果你使用@Table(name=“MyTableName”),唯一的真正目的是链接一个实体,如果它有非常不同的表名,比如说将hibernate添加到一个非常传统的系统?与实际的遗留表名“factory_personel”类似,但在代码中,已选择将表表示为“FactoryWorker”?实际上,这取决于选择,您也可以实现命名策略。请参见此处了解详细说明,案例转换是一个额外的功能,因为CamelCase是数据库的Java&snake_案例中的一个通用约定。