CAPS LOCK CAPS LOCK - 24 days ago 10
Java Question

Hibernate schema is automatically dropped on deploy

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

Answer

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.