gpt4 book ai didi

java - Hibernate 连接到具有相同表的多个数据库

转载 作者:行者123 更新时间:2023-11-29 13:50:14 26 4
gpt4 key购买 nike

我必须连接到包含相同表的两个数据库(PostgreSQL、Oracle)。当我在不同的包中创建相同表的实体时,它不起作用。

即使使用两个数据库连接,应用程序始终只指向一个数据库连接。

在 Hibernate 中是否可以连接到来自不同数据库的相同表?

application.properties

#DataSource settings for Postgres
datasource.secondary.url =jdbc:postgresql://localhost:5433/****
datasource.secondary.username =postgres
datasource.secondary.password =Postgre@1234
datasource.secondary.driverClassName=org.postgresql.Driver
datasource.secondary.dialect=org.hibernate.dialect.PostgreSQLDialect

#DataSource settings for oracle
datasource.primary.url = jdbc:oracle:thin:@localhost:1521:xe
datasource.primary.username = ***
datasource.primary.password = ***
datasource.primary.driverClassName=oracle.jdbc.OracleDriver

配置

@Configuration
public class MultipleDBConfig {

@Primary
@Bean(name = "oracleDb")
@ConfigurationProperties(prefix = "datasource.primary")
public DataSource mysqlDataSource() {
return DataSourceBuilder.create().build();
}


@Bean(name = "postgresDb")
@ConfigurationProperties(prefix = "datasource.secondary")
public DataSource postgresDataSource() {
return DataSourceBuilder.create().build();
}

}

主要

@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "primaryEntityManager",
transactionManagerRef = "primaryEntityManagerFactory",
basePackages = {"com.ubl.model.*"})
public class PrimaryDBConfig {

@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] {"com.ubl.model.migration.entity.oracle"});
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalJpaProperties());
em.setPersistenceUnitName("customers");

return em;
}

Properties additionalJpaProperties(){
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
properties.setProperty("hibernate.show_sql", "true");

return properties;
}

@Bean
public DataSource dataSource(){
return DataSourceBuilder.create()
.url("jdbc:oracle:thin:@localhost:1521:xe")
.driverClassName("oracle.jdbc.OracleDriver")
.username("****")
.password("****")
.build();
}

@Bean(name = "primarytransactionManager")
public JpaTransactionManager transactionManager(EntityManagerFactory customerEntityManager){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(customerEntityManager);

return transactionManager;
}

}

中学

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "secondaryEntityManagerFactory",
transactionManagerRef = "secondaryTransactionManager",
basePackages = {"com.ubl.*"})
public class SecondaryDBConfig {

@Autowired
JpaVendorAdapter jpaVendorAdapter;

@Value("${datasource.secondary.url}")
private String databaseURL;

@Value("${datasource.secondary.username}")
private String username;

@Value("${datasource.secondary.password}")
private String password;

@Value("${datasource.secondary.driverClassName}")
private String driverClassName;

@Value("${datasource.secondary.dialect}")
private String dialect;

public SecondaryDBConfig() {
System.out.println("Secondary repository");
System.out.println("driverClassName: *************" +driverClassName);
}

public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource(databaseURL, username, password);
dataSource.setDriverClassName(driverClassName);
return dataSource;
}

@Bean(name = "secondaryEntityManager")
public EntityManager entityManager() {
return entityManagerFactory().createEntityManager();
}

@Bean(name = "secondaryEntityManagerFactory")
public EntityManagerFactory entityManagerFactory() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", dialect);

LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setJpaVendorAdapter(jpaVendorAdapter);
emf.setPackagesToScan("com.ubl.model.*"); // package for entities
emf.setPersistenceUnitName("secondaryPersistenceUnit");
emf.setJpaProperties(properties);
emf.afterPropertiesSet();
return emf.getObject();
}

@Bean(name = "secondaryTransactionManager")
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}


}

当我运行应用程序时,出现以下错误:Caused by: org.hibernate.tool.schema.extract.spi.SchemaExtractionException: 在命名空间 (, ) 中找到多个表

最佳答案

您的第二个配置似乎使用与第一个配置相同的命名空间:

basePackages = {"com.ubl.model.*"}
basePackages = {"com.ubl.*"}

一旦您的第二个配置查找它的实体,它就会发现与第一个配置相同的实体,从而导致异常。您将希望将您的实体和您的配置分开。

basePackages = {"com.ubl.model.datasource1"}
basePackages = {"com.ubl.model.datasource2"} // well you get the idea and will find better names ;)

然后将所有实体移动到相应的文件夹中。虽然表是“相同的”,但每个单独的表都需要一个@Entity-Class,即使您要使用的表在结构上是相同的。

关于java - Hibernate 连接到具有相同表的多个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42388819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com