Jpa多数据源,为每个数据源配置单独的命名策略

起因:

  • spring data jpa多数据源配置可以参考Jpa多数据源配置,我就是参考这篇文章配置好的。
  • 我的数据源有两个,一个是mysql,一个是sqlserver。
  • mysql数据库的表字段形式采用的是jpa默认的命名策略,实体属性采用currentUserName形式的驼峰命名,映射时自动将大写转换为小写,并用_(下划线)连接,即current_user_name
  • 但是sqlserver数据库中表中字段的命名为CurrentUserName这种形式。所以在映射的时候会报异常--无效的列名。即使在相关实体的字段上添加@column注解,但是仍然无效。
  • 经过查找资料,原因是因为jpa默认的命名策略为spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy,采用这种命名策略,@column注解会失效。网上的解决办法都是单数据源的配置,都不能解决我当前项目遇到的问题。
  • 无奈只能自己解决。

解决办法

  • 依照上面的参考文章,多数据源配置中有这么一段代码,如下:
    //注入JPA配置实体
    @Autowired
    private JpaProperties jpaProperties;

    //获取jpa配置信息
    private Map getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

  • return jpaProperties.getHibernateProperties(dataSource);这段代码前加上以下代码:
jpaProperties.getHibernate().getNaming().setPhysicalStrategy("org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");

即:

//获取jpa配置信息
    private Map getVendorProperties(DataSource dataSource) {
        jpaProperties.getHibernate().getNaming().setPhysicalStrategy("org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        return jpaProperties.getHibernateProperties(dataSource);
    }

  • jpa的默认配置有两个,可以根据需要做选择:
    //spring物理命名策略--不对属性名做任何改变
    private static final String DEFAULT_PHYSICAL_STRATEGY = "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy";
    
    //spring隐式命名策略--对属性名做下划线连接处理,并将大写改为小写
    private static final String DEFAULT_IMPLICIT_STRATEGY = "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy";

  • jpa默认的命名策略不能够满足可以自定义命名策略,网上有相关的示例,不再赘述。

我们看下这句配置所涉及的源码:
  • HibernateNamingJpaProperties的两个内部类
@ConfigurationProperties(prefix = "spring.jpa")
public class JpaProperties {

       ......省略

       private Hibernate hibernate = new Hibernate();

       public Hibernate getHibernate() {
          return this.hibernate;
       }

      ......省略

  ......省略
  public static class Hibernate {

          private static final String USE_NEW_ID_GENERATOR_MAPPINGS = "hibernate.id."
                  + "new_generator_mappings";

          public Naming getNaming() {
              return this.naming;
          }
          ......省略
  }

  ......省略
  public static class Naming {

          private static final String DEFAULT_PHYSICAL_STRATEGY = "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy";

          private static final String DEFAULT_IMPLICIT_STRATEGY = "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy";

          /**
           * Fully qualified name of the physical naming strategy.
           */
          private String physicalStrategy;

          public void setPhysicalStrategy(String physicalStrategy) {
              this.physicalStrategy = physicalStrategy;
          }
          
          private void applyNamingStrategies(Map properties) {
              applyNamingStrategy(properties, "hibernate.implicit_naming_strategy",
                    this.implicitStrategy, DEFAULT_IMPLICIT_STRATEGY);
              applyNamingStrategy(properties, "hibernate.physical_naming_strategy",
                    this.physicalStrategy, DEFAULT_PHYSICAL_STRATEGY);
          }

          private void applyNamingStrategy(Map properties, String key,
                  String strategy, String defaultStrategy) {
              if (strategy != null) {
                  properties.put(key, strategy);
              }
              else if (defaultStrategy != null && !properties.containsKey(key)) {
                  properties.put(key, defaultStrategy);
              }
          }
          ......省略
  }
}
  • 通过连续的调用获取到Naming对象,设置命名策略的属性physicalStrategy就在此对象中。
  • return jpaProperties.getHibernateProperties(dataSource);最后返回的这句中,最终会去调用applyNamingStrategies()此方法,我们在上一步已经将strategy设置过了,在applyNamingStrategy()这个方法中会去判断,它不为空,就会将此putproperties中去。如果我们不去配置这个策略,如源码所示,它会设置默认的命名策略。

如有错误,欢迎指正。

你可能感兴趣的:(Jpa多数据源,为每个数据源配置单独的命名策略)