5

Looking at the questions (for instance, this one) related to Hibernate schema generator led me to the conclusion that the property that sets behaviour is hibernate.hbm2ddl.auto.

However, it seems to be ignored since it does not matter what the value is - the schema is always exported and the tables are always dropped when the spring boot application is deployed to wildfly.

The following code contains imports and configuration beans for the H2 data source and Hibernate session factory.

import javax.sql.DataSource;
import java.util.Properties;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Bean
public DataSource dataSource() {
    return new DriverManagerDataSource(h2Connection, h2Username, h2Password);
}

@Bean
public LocalSessionFactoryBean sessionFactory() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    properties.put("hibernate.show_sql", true);
    properties.put("hibernate.hbm2ddl.auto", "validate");

    LocalSessionFactoryBean localSessionFactory = new LocalSessionFactoryBean();
    localSessionFactory.setDataSource(dataSource());
    localSessionFactory.setHibernateProperties(properties);
    localSessionFactory.setAnnotatedClasses(new Class[] { Account.class, Product.class, Subscription.class });

    return localSessionFactory;
}

I've been working on this one for past few days but I still haven't found the right solution. Otherwise hibernate works fine.

The problem is how to disable table dropping on each deploy so the data in the H2 database won't be lost.

UPDATE It looks like the tables are dropped when the session is closed (new deploy is started)

2015-11-05 04:39:15 INFO  AnnotationMBeanExporter:449 - Unregistering JMX-exposed beans on shutdown
2015-11-05 04:39:15 DEBUG SessionFactoryImpl:1339 - HHH000031: Closing
2015-11-05 04:39:15 DEBUG BootstrapServiceRegistryImpl:308 - Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries
2015-11-05 04:39:15 DEBUG AbstractServiceRegistryImpl:406 - Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries
2015-11-05 04:39:15 INFO  LocalContainerEntityManagerFactoryBean:462 - Closing JPA EntityManagerFactory for persistence unit 'default'
2015-11-05 04:39:15 DEBUG SessionFactoryImpl:1339 - HHH000031: Closing
2015-11-05 04:39:15 INFO  SchemaExport:344 - HHH000227: Running hbm2ddl schema export
2015-11-05 04:39:15 DEBUG SchemaExport:354 - Import file not found: /import.sql
2015-11-05 04:39:15 DEBUG SQL:109 - drop table account if exists
2015-11-05 04:39:15 DEBUG SQL:109 - drop table product if exists
2015-11-05 04:39:15 DEBUG SQL:109 - drop table subscription if exists
2015-11-05 04:39:15 INFO  SchemaExport:406 - HHH000230: Schema export complete
Community
  • 1
  • 1
CAPS LOCK
  • 1,960
  • 3
  • 26
  • 41
  • 1
    Put there `properties.put("hibernate.hbm2ddl.auto", "none");` which is not really valid parameter it will complain about it but will not perform any actions. Also make sure you don't use H2 as in memory database... – Milkmaid Nov 10 '15 at 08:58
  • @Milkmaid: H2 is stored in the file. Putting `none` as the value doesn't help. It looks like the property is ignored. Also, I've updated the question. – CAPS LOCK Nov 10 '15 at 09:16

3 Answers3

5

The thing is that spring boot uses spring.jpa. So the right properties are

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect

Here is the GitHub repository that led me to the solution.

CAPS LOCK
  • 1,960
  • 3
  • 26
  • 41
0

The SessionFactoryImpl has got this code inside it's close method. This method is called when the SessionFactory is being closed.

if ( settings.isAutoDropSchema() ) {
            schemaExport.drop( false, true );
        }

And the relevant settings are picked up in SetttingsFactory as

String autoSchemaExport = properties.getProperty( AvailableSettings.HBM2DDL_AUTO );
        if ( "validate".equals(autoSchemaExport) ) {
            settings.setAutoValidateSchema( true );
        }
        if ( "update".equals(autoSchemaExport) ) {
            settings.setAutoUpdateSchema( true );
        }
        if ( "create".equals(autoSchemaExport) ) {
            settings.setAutoCreateSchema( true );
        }
        if ( "create-drop".equals( autoSchemaExport ) ) {
            settings.setAutoCreateSchema( true );
            settings.setAutoDropSchema( true );
        }
Shailendra
  • 8,874
  • 2
  • 28
  • 37
0

just u need to turn if the mapping in "hibernate.cfg.xml" , by doing this it wont delete and re-create

<property name="hibernate.hbm2ddl.auto" value="update"/>
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43