How to turn off hbm2ddl?

不打扰是莪最后的温柔 提交于 2019-11-26 15:44:24

问题


I couldn't find a reference on how to switch hbm2ddl off.


回答1:


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




回答2:


You can switch it off by :

hibernate.hbm2ddl.auto=none

It's undocumented but priceless !




回答3:


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.




回答4:


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>



回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/3179765/how-to-turn-off-hbm2ddl

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