Hibernate架构在部署时自动删除

时间:2015-11-10 08:54:31

标签: java spring hibernate

查看与Hibernate模式生成器相关的问题(例如,this one)使我得出结论:设置行为的属性是hibernate.hbm2ddl.auto

但是,它似乎被忽略了,因为它的值是什么并不重要 - 总是导出模式,并且当spring boot应用程序部署到wildfly时,表总是被删除。

以下代码包含H2数据源和Hibernate会话工厂的导入和配置Bean。

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;
}

过去几天我一直在研究这个问题,但我仍然找不到合适的解决方案。否则hibernate工作正常。

问题是如何在每次部署时禁用表删除,这样H2数据库中的数据就不会丢失。

更新 在会话关闭(启动新部署)时,看起来表会被删除

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

3 个答案:

答案 0 :(得分:3)

问题是spring boot使用spring.jpa。所以正确的属性是

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

以下GitHub repository引导我找到解决方案。

答案 1 :(得分:0)

SessionFactoryImpl已将此代码纳入其close方法。 <{3}}被关闭时会调用此方法。

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

相关设置在SessionFactory中作为

获取
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 );
        }

答案 2 :(得分:0)

如果你需要在 “hibernate.cfg.xml” 中进行映射,这样做就不会删除并重新创建

<property name="hibernate.hbm2ddl.auto" value="update"/>