【发布时间】:2019-01-21 22:55:21
【问题描述】:
我在使用多数据源时遇到了问题。我扩展 AbstractRoutingDataSource 并将其创建为 bean。像这样:
@Configuration
public class JpaConfiguration{
@Bean
public void DataSource dataSource(){
return new AbstractRoutingDataSource(){...}
}
在我的 yml 文件中,我设置了 spring.jpa.hibernate.ddl-auto update。
spring:
...
jpa:
hibernate:
ddl-auto: update
...
一切顺利。但是当我更改数据源时,就像另一个架构一样。我必须自己创建表。当我更改数据源或将数据源添加到我的routingDataSource 运行时,有什么方法可以让休眠自动创建表?
-----更新----
spring boot 自动创建LocalContainerEntityManagerFactoryBean,应用启动时使用defaultDataSource,这里是AbstractRoutingDataSource中的代码:
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = determineCurrentLookupKey();
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
}
return dataSource;
}
使用默认数据源,休眠自动创建表。但是当我使用另一个数据源时,它不起作用。
【问题讨论】:
标签: hibernate spring-boot spring-data-jpa