gpt4 book ai didi

java - Hibernate 架构在部署时自动删除

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:52 24 4
gpt4 key购买 nike

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

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

以下代码包含 H2 数据源和 Hibernate session 工厂的导入和配置 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 数据库中的数据就不会丢失。

更新看起来当 session 关闭时表被删除(新部署开始)

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

最佳答案

问题是 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这让我找到了解决方案。

关于java - Hibernate 架构在部署时自动删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33626089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com