How to turn off hbm2ddl?

☆樱花仙子☆ 提交于 2019-11-27 07:02:53
Pascal Thivent

Just omitting hibernate.hbm2ddl.auto defaults to Hibernate not doing anything. From the reference documentation:

1.1.4. Hibernate configuration

The hbm2ddl.auto option turns on automatic generation of database schemas directly into the database. This can also be turned off by removing the configuration option, or redirected to a file with the help of the SchemaExport Ant task.

Setting hbm2ddl.auto to none (undocumented) might generate a warning, such as: org.hibernate.cfg.SettingsFactory - Unrecognized value for "hibernate.hbm2ddl.auto": none

You can switch it off by :

hibernate.hbm2ddl.auto=none

It's undocumented but priceless !

To get this one clear, one should look into the source of org.hibernate.cfg.SettingsFactory (you might see something else depending on the version used):

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 );
}

In the org.hibernate.cfg.Settings class those variables are initialized as:

private boolean autoCreateSchema;
private boolean autoDropSchema;
private boolean autoUpdateSchema;
private boolean autoValidateSchema;

so these default to false.

Omitting the hibernate.hbm2ddl.auto setting should switch off the HBM2DDL_AUTO functionality as would suggested hibernate.hbm2ddl.auto = none, but on the latter case you get a warning in the log.

in hibernate.properties

hibernate.hbm2ddl.auto=validate

Of course, the place to configure it depends on the way you configure your hibernate - if it is programatically, set the property there. If it is from hibernate.cfg.xml:

<property name="hibernate.hbm2ddl.auto">validate</property>

If you enter an unsupported value it will tell you which ones are supported: o.h.b.i.SessionFactoryBuilderImpl : Unrecognized hbm2ddl_auto value : bla. Supported values include 'create', 'create-drop', 'update', 'none' and 'validate'. Ignoring

And the value none is the default, is officially supported and documented: https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl

This property is not required. Just delete the hibernate.hbm2ddl.auto entry completely from the xml file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!