2017-02-22 8 views
1

同じテーブルを含む2つのデータベース(PostgreSQL、Oracle)に接続する必要があります。 異なるパッケージに同じテーブルのエンティティを作成すると、動作しません。Hibernateは同じテーブルを持つ複数のデータベースに接続します

2つのデータベース接続を使用しても、アプリケーションは常に1つのデータベース接続のみを指します。

Hibernateでは、異なるデータベースの同じテーブルに接続することは可能ですか?

application.properties

#DataSource settings for Postgres 
datasource.secondary.url =jdbc:postgresql://localhost:5433/**** 
datasource.secondary.username =postgres 
datasource.secondary.password [email protected] 
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:名前空間で見つかった複数のテーブル(、)

答えて

0

あなたの二config設定最初と同じ名前空間を使用するように表示されます。

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

2番目の設定は、それのエンティティを探したら、それは拳がするように同じことを発見し、これの元になります。エンティティと両方の設定を分離したいと思うでしょう。

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

次に、それぞれのエンティティをそれぞれのフォルダに移動します。テーブルは「同じ」ですが、使用するテーブルは構造的に同じですが、個々のテーブルごとに1つの@ Entity-Classが必要です。

関連する問題