看着与Hibernate模式生成器相关的问题(例如这个),我得出结论,设置行为的属性是
但是,似乎无论值是什么,它都被忽略了 - 模式总是被导出,并且当Spring Boot应用程序部署到WildFly时,表总是被删除。
以下代码包含H2数据源和Hibernate会话工厂的导入和配置bean。
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
properties.put("hibernate.hbm2ddl.auto", "none");
放在那里,虽然它不是真正有效的参数,但会引发警告而不执行任何操作。另外,请确保不要使用 H2 作为内存数据库... - Milkmaid