How to turn off hbm2ddl?

JavaHibernateOrmHbm2ddl

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

You can switch it off by :

hibernate.hbm2ddl.auto=none

It's undocumented but priceless !

Solution 3 - Java

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.

Solution 4 - Java

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>

Solution 5 - Java

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

Solution 6 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexView Question on Stackoverflow
Solution 1 - JavaPascal ThiventView Answer on Stackoverflow
Solution 2 - JavaJulien BRENELIEREView Answer on Stackoverflow
Solution 3 - JavaAndres SaaremõtsView Answer on Stackoverflow
Solution 4 - JavaBozhoView Answer on Stackoverflow
Solution 5 - JavajstadlerView Answer on Stackoverflow
Solution 6 - JavagdrtView Answer on Stackoverflow