我找不到如何关闭hbm2ddl的参考资料。
如果省略 hibernate.hbm2ddl.auto
配置,Hibernate 不会执行任何操作。参考文档如下:
1.1.4. Hibernate 配置
hbm2ddl.auto
选项可以开启直接向数据库自动生成表结构的功能。 通过移除该配置选项也可以关闭这一功能, 或者借助 SchemaExport Ant 任务将其重定向到文件中。
将 hbm2ddl.auto
设置为 none
(未记录在文档中)可能会生成警告信息,例如:org.hibernate.cfg.SettingsFactory - Unrecognized value for "hibernate.hbm2ddl.auto": none
hibernate.hbm2ddl.auto=none
这是不文档化的,但非常宝贵!
11:19:43.359 -ERROR [main] SpringApplication.reportFailure:833 - 应用运行失败 java.lang.IllegalArgumentException: 未识别的遗留'hibernate.hbm2ddl.auto'值:potato
- pdemSchemaManagementToolCoordinator.interpret
的源代码,其中值“none”在遗留值(以“hibernate.”开头)和jpa值上进行了明确测试,该值是javax.persistence.schema-generation.database.action
与javax.persistence.schema-generation.scripts.action
协调。感谢您指出了新的javax值,以替换hibernate值。 - pdemorg.hibernate.cfg.SettingsFactory
的源代码(根据使用的版本可能会看到其他内容)。String autoSchemaExport = properties.getProperty( AvailableSettings.HBM2DDL_AUTO );
if ( "validate".equals(autoSchemaExport) ) {
settings.setAutoValidateSchema( true );
}
else if ( "update".equals(autoSchemaExport) ) {
settings.setAutoUpdateSchema( true );
}
else if ( "create".equals(autoSchemaExport) ) {
settings.setAutoCreateSchema( true );
}
else if ( "create-drop".equals( autoSchemaExport ) ) {
settings.setAutoCreateSchema( true );
settings.setAutoDropSchema( true );
}
else if ( !StringHelper.isEmpty( autoSchemaExport ) ) {
LOG.warn( "Unrecognized value for \"hibernate.hbm2ddl.auto\": " + autoSchemaExport );
}
在org.hibernate.cfg.Settings
类中,这些变量被初始化为:
private boolean autoCreateSchema;
private boolean autoDropSchema;
private boolean autoUpdateSchema;
private boolean autoValidateSchema;
因此,这些默认为false。
省略hibernate.hbm2ddl.auto
设置应该关闭HBM2DDL_AUTO
功能,就像建议的hibernate.hbm2ddl.auto = none
一样,但在后一种情况下,您会在日志中收到警告。
在hibernate.properties文件中
hibernate.hbm2ddl.auto=validate
<property name="hibernate.hbm2ddl.auto">validate</property>
o.h.b.i.SessionFactoryBuilderImpl:未识别 hbm2ddl_auto 值:bla。支持的值包括 'create'、'create-drop'、'update'、'none' 和 'validate'。忽略
而 none
是默认值,被正式支持和记录在文档中:
https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl
hibernate.hbm2ddl.auto
条目即可。
hibernate-core
jar 包。枚举org.hibernate.boot.SchemaAutoTooling
显示了可能的值。我的版本是 5.3.9,包含NONE
。 - ygor