如何关闭hbm2ddl?

Ale*_*lex 56 java orm hibernate hbm2ddl

我找不到关于如何关闭hbm2ddl的参考.

Pas*_*ent 76

只是省略hibernate.hbm2ddl.auto了Hibernate的默认值而没有做任何事情.从参考文档:

1.1.4.Hibernate配置

hbm2ddl.auto选项可以直接将数据库模式自动生成到数据库中. 这也可以通过删除配置选项来关闭,或者在SchemaExport Ant任务的帮助下重定向到文件.

设置hbm2ddl.autonone(未记录)可能会生成警告,例如:org.hibernate.cfg.SettingsFactory - Unrecognized value for "hibernate.hbm2ddl.auto": none

  • 希望,***none***现在是一个有效值(至少从5.1.2.Final开始). (5认同)

小智 35

您可以通过以下方式将其关闭:

hibernate.hbm2ddl.auto=none
Run Code Online (Sandbox Code Playgroud)

它没有证件但是无价!

  • 你也可以写[hibernate.hbm2ddl.auto = potato](http://stackoverflow.com/a/10635006/1113392),这会产生同样的效果. (61认同)
  • 这将导致`WARN org.hibernate.cfg.SettingsFactory - "hibernate.hbm2ddl.auto":none`的无法识别的值(使用版本4.3.11.Final时).把它留空吧. (4认同)

小智 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 );
}
Run Code Online (Sandbox Code Playgroud)

org.hibernate.cfg.Settings类中,这些变量初始化为:

private boolean autoCreateSchema;
private boolean autoDropSchema;
private boolean autoUpdateSchema;
private boolean autoValidateSchema;
Run Code Online (Sandbox Code Playgroud)

所以这些默认为false.

省略hibernate.hbm2ddl.auto设置应该HBM2DDL_AUTO按照建议关闭功能hibernate.hbm2ddl.auto = none,但在后一种情况下,您会在日志中收到警告.


Boz*_*zho 5

在hibernate.properties中

hibernate.hbm2ddl.auto=validate
Run Code Online (Sandbox Code Playgroud)

当然,配置它的位置取决于您配置休眠的方式-如果是编程方式,请在此处设置属性。如果来自hibernate.cfg.xml:

<property name="hibernate.hbm2ddl.auto">validate</property>
Run Code Online (Sandbox Code Playgroud)