背景

  • 在使用SpringBoot配置多数据源时,找到了很多大牛的资料,结果在使用jpaProperties.getHibernateProperties()时一直报错。
  • 很多资料写法如下,可是我这里就行不通
    • @Primary
          @Bean(name = "entityManagerFactoryPrimary")
          public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
              return builder
                      .dataSource(primaryDataSource)// 设置数据源
                      .properties(jpaProperties.getProperties())// 设置jpa配置
                      .properties(getVendorProperties())// 设置hibernate配置
                      .packages("com.xxxx.xxxxxx.xxxxxx.xxxxx.entities.superviser") //设置实体类所在位置
                      .persistenceUnit("primaryPersistenceUnit")// 设置持久化单元名,用于@PersistenceContext注解获取EntityManager时指定数据源
                      .build();
          }
      
          private Map getVendorProperties() {
              return jpaProperties.getHibernateProperties(new HibernateSettings());
          }

原因

  • 我使用的SpringBoot版本是2.2.1 RELEASE 版本,这个getHibernateProperties()方法已经被PASS掉了。

解决办法

  • 曲线救国,使用如下代码即可
    • @Primary
          @Bean(name = "entityManagerFactoryPrimary")
          public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
              return builder
                      .dataSource(primaryDataSource)// 设置数据源
                      .properties(jpaProperties.getProperties())// 设置jpa配置
                      .properties(getVendorProperties())// 设置hibernate配置
                      .packages("com.canaan.superviser.objects.rpc.entities.superviser") //设置实体类所在位置
                      .persistenceUnit("primaryPersistenceUnit")// 设置持久化单元名,用于@PersistenceContext注解获取EntityManager时指定数据源
                      .build();
          }
      
      
          private Map getVendorProperties() {
              return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
      
      //        return jpaProperties.getHibernateProperties(new HibernateSettings());
          }

       

相关文章:

  • 2022-01-16
  • 2021-04-03
  • 2021-07-13
  • 2022-12-23
  • 2021-10-16
  • 2021-09-13
  • 2021-06-24
  • 2021-11-14
猜你喜欢
  • 2021-06-16
  • 2021-09-15
  • 2021-11-14
  • 2019-10-27
  • 2021-12-30
  • 2021-11-08
  • 2022-01-21
相关资源
相似解决方案