Hibernate在部署时会自动删除架构。

5
看着与Hibernate模式生成器相关的问题(例如这个),我得出结论,设置行为的属性是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

1
properties.put("hibernate.hbm2ddl.auto", "none"); 放在那里,虽然它不是真正有效的参数,但会引发警告而不执行任何操作。另外,请确保不要使用 H2 作为内存数据库... - Milkmaid
@Milkmaid:H2存储在文件中。将“none”作为值不起作用。看起来该属性被忽略了。另外,我已经更新了问题。 - CAPS LOCK
3个回答

5
问题在于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代码库.


0

SessionFactoryImpl中的代码在其close方法中。当SessionFactory被关闭时,将调用此方法。

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

相关设置在SetttingsFactory中被捕获

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

我明白,但无论我设置什么值(模式始终会被删除),都没有关系。 - CAPS LOCK
那为什么不使用IDE和调试器来查看会话工厂关闭时它选择了什么值呢? - Shailendra

0

你只需要在"hibernate.cfg.xml"文件中更改映射,这样就不会删除和重新创建了

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接