簡體   English   中英

Hibernate使用相同的表連接到多個數據庫

[英]Hibernate connect to multiple database with same tables

我必須連接到包含相同表的兩個數據庫(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());
    }


}

當我運行應用程序時,我得到以下錯誤:引起: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,即使您要使用的表在結構上完全相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM