Всем добрый вечер.
Подключаю две базы в одном проекте.
Проблема в том, что в в первой ДБ
first
создает таблица fruit из второй ДБ
second
.
Подскажите, пожалуйста, где косяк?
model_1
@Entity
@Data
@Table(schema = "first", name = "smoky")
public class Smoky {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String value;
public Smoky() {
}
public Smoky(Long id, String value) {
this.id = id;
this.value = value;
}
}
model_2
@Entity
@Data
@Table(schema = "second", name = "fruit")
public class Fruit {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long ids;
private String dist;
public Fruit() {
}
public Fruit(Long ids, String dist) {
this.ids = ids;
this.dist = dist;
}
}
datasource_1
@Configuration
@PropertySource({"classpath:application.properties"})
@EnableJpaRepositories(
basePackages = "com.example.mailsender.first_db.Repository",
entityManagerFactoryRef = "firstEntityManager",
transactionManagerRef = "firstTransactionManager"
)
public class CustomerPersistenceConfiguration {
@Autowired
private Environment env;
@Primary
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource customerDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean customerEntityManager() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(customerDataSource());
em.setPackagesToScan("com.example.mailsender.first_db.Model");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
@Primary
public PlatformTransactionManager customerTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(customerEntityManager().getObject());
return transactionManager;
}
}
datasource_2
@Configuration
@PropertySource({"classpath:application.properties"})
@EnableJpaRepositories(
basePackages = "com.example.mailsender.second_db.Repository",
entityManagerFactoryRef = "secondEntityManager",
transactionManagerRef = "secondTransactionManager"
)
public class ProductPersistenceConfiguration {
@Autowired
private Environment env;
@Bean
@ConfigurationProperties(prefix="second.datasource")
public DataSource productDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean productEntityManager() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(productDataSource());
em.setPackagesToScan("com.example.mailsender.second_db.Model");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
public PlatformTransactionManager productTransactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(productEntityManager().getObject());
return transactionManager;
}
}
prop
#db settings
spring.datasource.url=jdbc:mysql://localhost:3306/first
spring.datasource.username=user
spring.datasource.password=user
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
second.datasource.url=jdbc:mysql://localhost:3306/second
second.datasource.username=user
second.datasource.password=user
second.jpa.show-sql=true
second.jpa.properties.hibernate.format_sql=true
second.datasource.driver-class-name=com.mysql.cj.jdbc.Driver