我在非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; }