如何通过使用 JPA + Hibernate 和 Spring-boot 在一个数据库中使用多个模式?

Posted

技术标签:

【中文标题】如何通过使用 JPA + Hibernate 和 Spring-boot 在一个数据库中使用多个模式?【英文标题】:How to use multiple schema's in one database by using JPA + Hibernate with Spring-boot? 【发布时间】:2018-11-18 12:48:44 【问题描述】:

我需要它在一个数据库(mysql)中访问 2 个不同的架构。

我在这里写了两个配置类:

package twodb.webfi.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;   
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="twodb.webfi","twodb.mc",
entityManagerFactoryRef = "entityManagerFactory1", 
transactionManagerRef = "transactionManager1",
considerNestedRepositories = true)
public class FirstConfig 

@Autowired
private Environment env;

@Bean
public PlatformTransactionManager transactionManager1()

    EntityManagerFactory factory = entityManagerFactory1().getObject();
    return new JpaTransactionManager(factory);


@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory1()

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(Boolean.TRUE);
    vendorAdapter.setShowSql(Boolean.TRUE);

    factory.setDataSource(dataSource1());
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan(new String[] "twodb.webfi.entities","twodb.mc.model");
    Properties jpaProperties = new Properties();

    jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.ddl-auto"));
    jpaProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
    jpaProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.showSql"));
    factory.setJpaProperties(jpaProperties);

    factory.afterPropertiesSet();
    factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
    return factory;


@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator()

    return new HibernateExceptionTranslator();



@Bean(destroyMethod = "close")
 public HikariDataSource dataSource1() 
    System.out.println("<--HikariDataSource1-->");
        HikariConfig dataSourceConfig = new HikariConfig();
        dataSourceConfig.setDriverClassName(env.getProperty("ui.db.driver"));
        dataSourceConfig.setJdbcUrl(env.getProperty("ui.db.url"));
        dataSourceConfig.setUsername(env.getProperty("ui.db.username"));
        dataSourceConfig.setPassword(env.getProperty("ui.db.password"));           

        dataSourceConfig.setMaximumPoolSize(env.getProperty("hikari.maximumPoolSize", Integer.class, new Integer(1)));
        dataSourceConfig.setMinimumIdle(env.getProperty("hikari.minimumIdle", Integer.class, new Integer(1)));
        dataSourceConfig.setIdleTimeout(env.getProperty("hikari.idleTimeout", Long.class, new Long(600000)));   // 10 min
        dataSourceConfig.setMaxLifetime(env.getProperty("hikari.maxLifetime", Long.class, new Long(1800000)));  // 30 min
        dataSourceConfig.setPoolName(env.getProperty("hikari.poolName", "upiCp"));

        return new HikariDataSource(dataSourceConfig);
    

 

这里是另一个配置类:

package twodb.webfi.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="twodb.webfi","twodb.mc",
entityManagerFactoryRef = "entityManagerFactory2", 
transactionManagerRef = "transactionManager2",
considerNestedRepositories = true)
public class SecondConfig 

@Autowired
private Environment env;    

@Bean
public PlatformTransactionManager transactionManager2()

    EntityManagerFactory factory = entityManagerFactory2().getObject();
    return new JpaTransactionManager(factory);


@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory2()

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(Boolean.TRUE);
    vendorAdapter.setShowSql(Boolean.TRUE);

    factory.setDataSource(dataSource2());
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan(new String[] "twodb.webfi.entities","twodb.mc.model");
    Properties jpaProperties = new Properties();

    jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.ddl-auto"));
    jpaProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
    jpaProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.showSql"));
    factory.setJpaProperties(jpaProperties);

    factory.afterPropertiesSet();
    factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
    return factory;


@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator()

    return new HibernateExceptionTranslator();
   

@Bean(destroyMethod = "close")
 public HikariDataSource dataSource2() 
    System.out.println("<--HikariDataSource2-->");
        HikariConfig dataSourceConfig = new HikariConfig();
        dataSourceConfig.setDriverClassName(env.getProperty("web.db.driver"));
        dataSourceConfig.setJdbcUrl(env.getProperty("web.db.url"));
        dataSourceConfig.setUsername(env.getProperty("web.db.username"));
        dataSourceConfig.setPassword(env.getProperty("web.db.password"));           

        dataSourceConfig.setMaximumPoolSize(env.getProperty("hikari.maximumPoolSize", Integer.class, new Integer(1)));
        dataSourceConfig.setMinimumIdle(env.getProperty("hikari.minimumIdle", Integer.class, new Integer(1)));
        dataSourceConfig.setIdleTimeout(env.getProperty("hikari.idleTimeout", Long.class, new Long(600000)));   // 10 min
        dataSourceConfig.setMaxLifetime(env.getProperty("hikari.maxLifetime", Long.class, new Long(1800000)));  // 30 min
        dataSourceConfig.setPoolName(env.getProperty("hikari.poolName", "upiCp"));

        return new HikariDataSource(dataSourceConfig);
    

  

在这里我遇到了异常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webCommonDaoImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available: expected single matching bean but found 2: entityManagerFactory1,entityManagerFactory

谁能指出我如何通过使用 JPA + Hibernate 和 Spring-boot 来使用多个数据库的正确方向?

我是 mysql 中的 uiweb 架构。两者都是同一个数据库。

【问题讨论】:

使用 @Primary 作为数据源之一,transactionmanagers 我可以在哪里使用这个@primary medium.com/@joeclever/… 这对我有用。要求:spring boot 2.2.11.RELEASE ***.com/a/67716454/16044144 这个解决方案对我有用!!! enter link description here 【参考方案1】:

使用限定符和 bean 名称将避免任何冲突。

我在我的应用程序 yaml 文件中创建了不同的数据源配置,如下所示

db-abc:
  datasource:
  url: $ABC_DATABASE_URL
  username: $ABC_DATABASE_USERNAME
  password: $ABC_DATABASE_PASSWORD
  driverClassName: org.postgresql.Driver
db-xyz:
  datasource:
  url: $XYZ_DATABASE_URL
  username: $XYZ_DATABASE_USERNAME
  password: $XYZ_DATABASE_PASSWORD
  driverClassName: org.postgresql.Driver
db-batch:
  datasource:
  url: $DATABASE_URL
  username: $DATABASE_USERNAME
  password: $DATABASE_PASSWORD
  driverClassName: org.postgresql.Driver

并创建了我的数据源配置,如下所示

@Configuration
public class DataSourceConfig 

private final Environment env;

@Autowired
public DataSourceConfig(Environment env) 
    this.env = env;


@Bean(name = "abcDataSource")
@ConfigurationProperties(prefix = "db-abc.datasource")
public DataSource abcDataSource() 
    return DataSourceBuilder
            .create()
            .url(env.getProperty("db-abc.datasource.url"))
            .driverClassName(env.getProperty("db-abc.datasource.driverClassName"))
            .username(env.getProperty("db-abc.datasource.username"))
            .password(env.getProperty("db-abc.datasource.password"))
            .build();


@Bean(name = "xyzDataSource")
@ConfigurationProperties(prefix = "db-xyz.datasource")
public DataSource xyzDataSource() 
    return DataSourceBuilder
            .create()
            .url(env.getProperty("db-xyz.datasource.url"))
            .driverClassName(env.getProperty("db-xyz.datasource.driverClassName"))
            .username(env.getProperty("db-xyz.datasource.username"))
            .password(env.getProperty("db-xyz.datasource.password"))
            .build();


@Primary
@Bean(name = "batchDataSource")
@ConfigurationProperties(prefix = "db-batch.datasource")
public DataSource batchDataSource() 
    return DataSourceBuilder
            .create()
            .url(env.getProperty("db-batch.datasource.url"))
            .driverClassName(env.getProperty("db-batch.datasource.driverClassName"))
            .username(env.getProperty("db-batch.datasource.username"))
            .password(env.getProperty("db-batch.datasource.password"))
            .build();


并使用相应的 bean 来做我想做的事情...使用限定符和 bean 名称将避免任何冲突。

对于单个数据库中的多个模式,您可以使用上述各自的模式

@Table(
    schema = "schema1", 
    name = "TBL_SCHEMA1_TABLE"
)
public class Schema1Entity implements Serializable                                                                             @Entity
@Table(
    schema = "schema2", 
    name = "TBL_SCHEMA2_TABLE"
)
public class Schema2Entity implements Serializable 

【讨论】:

对于单个数据库中的多个模式,您可以使用上述各自的模式 你如何将它与存储库一起使用(查找、保存……)?

以上是关于如何通过使用 JPA + Hibernate 和 Spring-boot 在一个数据库中使用多个模式?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Hibernate 和 JPA 处理填充下拉列表?

如何使用 JPA 和 Hibernate 删除带有 JOIN 的实体

如何使用 JPA 和 Hibernate 在非主键上连接表

如何使用 JPA 和 Hibernate 连接两个不相关的实体

数据库如何通过 Hibernate 与 jpa 注释的 pojo 类交互?

hibernate注解与jpa注解