Я пытаюсь настроить Spring Boot / Data для воссоздания таблиц при инициализации приложения. Для этого я настроил ddl-auto для создания. Я использую два источника данных.
Когда таблицы не существуют, они создаются. Но когда таблицы существуют, они не воссоздаются. Я думал, что ddl-auto create («создает схему, уничтожая предыдущие данные.») Всегда воссоздает таблицы.
application.yaml
spring:
profiles: spring
jpa:
generate-ddl: true
hibernate:
ddl-auto: create
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
format_sql: true
dba:
datasource:
url: jdbc:mysql://localhost:3306/DBA
username: root
password: 123
driver-class-name: com.mysql.cj.jdbc.Driver
dbb:
datasource:
url: jdbc:mysql://localhost:3306/DBB
username: root
password: 123
driver-class-name: com.mysql.cj.jdbc.Driver
Моя конфигурацияфайлы:
@SpringBootApplication
@ComponentScan( basePackages = {"com.example.mds"} )
public class EMdsApplication {
public static void main(String[] args) {
SpringApplication.run(EMdsApplication.class, args);
}
}
DBAConfig.java
@Configuration
@EntityScan(basePackages = {"com.example.mds.model"} )
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "dba_emf",
transactionManagerRef = "dba_tm",
basePackages = {"com.example.mds.repository"}
)
public class DBAConfig {
@Primary
@Bean(name = "dba_ds")
@ConfigurationProperties(prefix = "dba.datasource")
public DataSource dataSource(@Qualifier("dba_dsp") DataSourceProperties properties) {
return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
@Primary
@Bean(name = "dba_emf")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("dba_ds") DataSource datasource) {
return builder
.dataSource(datasource)
.packages("com.example.mds.model")
.persistenceUnit("dba_pu")
.build();
}
@Primary
@Bean(name = "dba_tm")
public PlatformTransactionManager transactionManager(@Qualifier("dba_emf") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Primary
@Bean(name = "dba_dsp")
@ConfigurationProperties(prefix = "dba.datasource")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
}
DBBConfig.java
@Configuration
@EntityScan(basePackages = {"com.example.mds.dbb.model"} )
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "dbb_emf",
transactionManagerRef = "dbb_tm",
basePackages = {"com.example.mds.dbb.repository"}
)
public class DBBConfig {
@Bean(name = "dbb_ds")
@ConfigurationProperties(prefix = "dbb.datasource")
public DataSource dataSource(@Qualifier("dbb_dsp") DataSourceProperties properties) {
return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
@Bean(name = "dbb_emf")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("dbb_ds") DataSource datasource) {
return builder
.dataSource(datasource)
.packages("com.example.mds.dbb.model")
.persistenceUnit("dbb_pu")
.build();
}
@Bean(name = "dbb_tm")
public PlatformTransactionManager transactionManager(@Qualifier("dbb_emf") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean(name = "dbb_dsp")
@ConfigurationProperties(prefix = "dbb.datasource")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
}