加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 以编程方式在springboot中设置hibernate.ddl-auto

发布时间:2020-12-15 04:31:18 所属栏目:Java 来源:网络整理
导读:我在非Web应用程序和数据jpa中使用 springboot.我使用除数据源之外的默认配置: private static final String dataSourceUrl = "jdbc:h2:./Database;DB_CLOSE_ON_EXIT=FALSE";@Beanpublic DataSource dataSource() { return DataSourceBuilder.create().url(
我在非Web应用程序和数据jpa中使用 springboot.我使用除数据源之外的默认配置:

private static final String dataSourceUrl = "jdbc:h2:./Database;DB_CLOSE_ON_EXIT=FALSE";
@Bean
public DataSource dataSource() {
    return DataSourceBuilder.create().url(dataSourceUrl).username("user").password("pwd").build();
}

如何以编程方式设置spring.jpa.hibernate.ddl-auto属性?

解决方法

添加以下bean似乎可以完成这项工作(感谢Jens的评论):

@Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);
    em.setPackagesToScan(new String[] { "packages.to.scan" });

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);

    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect","org.hibernate.dialect.H2Dialect");
    properties.setProperty("hibernate.hbm2ddl.auto","update");
    em.setJpaProperties(properties);

    return em;
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读