68

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

Ed Brannin
  • 7,691
  • 2
  • 28
  • 32
Alex
  • 4,033
  • 9
  • 37
  • 52

6 Answers6

89

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

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
42

You can switch it off by :

hibernate.hbm2ddl.auto=none

It's undocumented but priceless !

  • 79
    you might also write [hibernate.hbm2ddl.auto=potato](http://stackoverflow.com/a/10635006/1113392), this will have the same effect. – A4L Oct 23 '15 at 07:51
  • 4
    This will result in `WARN org.hibernate.cfg.SettingsFactory - Unrecognized value for "hibernate.hbm2ddl.auto": none` (when using version 4.3.11.Final). Just leave it empty. – Milanka Jan 04 '16 at 10:10
  • @A4L No potato! It crashes on SpringBoot 2 :`11:19:43.359 -ERROR [main ] SpringApplication.reportFailure:833 - Application run failed java.lang.IllegalArgumentException: Unrecognized legacy 'hibernate.hbm2ddl.auto' value : potato` – pdem Feb 16 '18 at 10:21
  • @pdem This setting is for hibernate not spring boot. Spring boot uses hibernate, please check which version of hibernate does spring boot 2 use. This answer is bases on an older version of hibernate, see link in my first comment. The actual stable version of hibernate is [5.2](http://hibernate.org/orm/releases/5.2/). Also please to [this answer](https://stackoverflow.com/a/26382600/1113392). Besides of that the exception reported here states that this is a legacy setting, which means there is an alternative for it and that you should be using it instead. – A4L Feb 16 '18 at 10:41
  • 1
    @A4L Yes, Spring Boot 2 RC1 uses Hibernate 5.1.12.Final. I just wanted to warn that your trick doesn't seem to work anymore for the latest version, but "none" is working fine. See the source of `SchemaManagementToolCoordinator.interpret`, where the value "none" is explicitly tested both on the legacy value (starting with "hibernate."), and the jpa value whìch is `javax.persistence.schema-generation.database.action` in coordination with `javax.persistence.schema-generation.scripts.action`. Thanks to have pointed me out the new javax value in replacement of the hibernate one. – pdem Feb 16 '18 at 12:26
12

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.

5

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>
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • That means the hbm2ddl can't switched off? What is the default value of hibernate.hbm2ddl.auto when it is not mentioned in the property file or hibernate.cfg.xml file? – Alex Jul 05 '10 at 13:40
  • http://stackoverflow.com/questions/438146/hibernate-question-hbm2ddl-auto-possible-values-and-what-they-do – Don Roby Jul 05 '10 at 13:45
  • @Alex - I assumed you have tried, and having problems. See Pascal's answer. "Validate" means that hibernate checks whether the mappings are consistent with the DB at launch time. – Bozho Jul 05 '10 at 13:52
3

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

jstadler
  • 340
  • 1
  • 3
  • 7
1

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

gdrt
  • 3,160
  • 4
  • 37
  • 56