如何关闭hbm2ddl?

68

我找不到如何关闭hbm2ddl的参考资料。

6个回答

90

如果省略 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


6
希望(至少从5.1.2.Final版本开始)none不再是有效值。 - Stephan
我在应用程序属性中省略了spring.jpa.hibernate.ddl-auto=false,错误已经消失。 - Chisimdi Damian Ezeanieto
1
请检查您的 hibernate-core jar 包。枚举 org.hibernate.boot.SchemaAutoTooling 显示了可能的值。我的版本是 5.3.9,包含 NONE - ygor

42
你可以通过以下方式关闭它:
hibernate.hbm2ddl.auto=none

这是不文档化的,但非常宝贵!


79
你也可以写成hibernate.hbm2ddl.auto=potato,这样做会产生相同的效果。 - A4L
4
这将导致在使用4.3.11.Final版本时出现“WARN org.hibernate.cfg.SettingsFactory - Unrecognized value for "hibernate.hbm2ddl.auto": none”的警告。只需将其留空即可。 - Milanka
@A4L 没有土豆!它在SpringBoot 2上崩溃了:11:19:43.359 -ERROR [main] SpringApplication.reportFailure:833 - 应用运行失败 java.lang.IllegalArgumentException: 未识别的遗留'hibernate.hbm2ddl.auto'值:potato - pdem
@pdem 这个设置是针对 hibernate 而不是 spring boot 的。Spring boot 使用 hibernate,请检查 spring boot 2 使用哪个版本的 hibernate。这个答案基于一个旧版本的 hibernate,可以在我的第一条评论中找到链接。实际稳定版本的 hibernate 是 5.2。此外,请参考 这个答案。除此之外,这里报告的异常说明这是一个遗留设置,这意味着有一个替代方案,你应该使用它来代替。 - A4L
1
@A4L 是的,Spring Boot 2 RC1使用Hibernate 5.1.12.Final。我只是想警告您,您的技巧似乎不再适用于最新版本,但“none”仍然可以正常工作。请参见SchemaManagementToolCoordinator.interpret的源代码,其中值“none”在遗留值(以“hibernate.”开头)和jpa值上进行了明确测试,该值是javax.persistence.schema-generation.database.actionjavax.persistence.schema-generation.scripts.action协调。感谢您指出了新的javax值,以替换hibernate值。 - pdem

12
为了清楚起见,您应查看org.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一样,但在后一种情况下,您会在日志中收到警告。


5

在hibernate.properties文件中

hibernate.hbm2ddl.auto=validate

当然,配置它的位置取决于您如何配置Hibernate - 如果是以编程方式进行配置,请在那里设置属性。如果是从hibernate.cfg.xml进行配置:
<property name="hibernate.hbm2ddl.auto">validate</property>

这意味着无法关闭hbm2ddl吗? 当属性文件或hibernate.cfg.xml文件中没有提及时,hibernate.hbm2ddl.auto的默认值是什么? - Alex
https://dev59.com/SHRC5IYBdhLWcg3wAcXU - Don Roby
@Alex - 我假设你已经尝试过,但遇到了问题。请查看Pascal的答案。“验证”意味着Hibernate在启动时检查映射是否与数据库一致。 - Bozho

3

1
这个属性不是必需的。只需完全删除xml文件中的hibernate.hbm2ddl.auto条目即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接