Spring Data JDBC自定义命名策略

huangapple go评论59阅读模式
英文:

Spring Data JDBC Custom Naming Strategy

问题

有没有办法更改引用实体的默认列命名策略,而不是使用默认的(类的名称)。文档说要在RelationalMappingContext的命名策略上调用setForeignKeyNaming(ForeignKeyNaming.IGNORE_RENAMING)方法。但是,该方法仅在实现DefaultNamingStrategyJdbcMappingContext的命名策略上可用,但它是包私有的(CachingNamingStrategy)。有没有其他办法解决这个问题?

英文:

Is there any way to change the default column naming strategy for a referenced entity then the default (name of the class). The docs say to call the method setForeignKeyNaming(ForeignKeyNaming.IGNORE_RENAMING) on RelationalMappingContext's NamingStrategy. However, this method is only available on the implementation DefaultNamingStrategy and JdbcMappingContext's naming strategy but it is package private (CachingNamingStrategy). Is there a way around this?

答案1

得分: 1

@Bean // 在某个配置类中
public JdbcMappingContext jdbcMappingContext() {
DefaultNamingStrategy namingStrategy = new DefaultNamingStrategy();
namingStrategy.setForeignKeyNaming(ForeignKeyNaming.IGNORE_RENAMING);
return new JdbcMappingContext(namingStrategy);
}

而不是默认的复数驼峰类名作为外键,可以自定义命名,只需确保字段名称相同。

英文:
@Bean // in some config class
public JdbcMappingContext jdbcMappingContext() {
    DefaultNamingStrategy namingStrategy = new DefaultNamingStrategy();
    namingStrategy.setForeignKeyNaming(ForeignKeyNaming.IGNORE_RENAMING);
    return new JdbcMappingContext(namingStrategy);
}

And instead of the default plural camelcase class name as a foreign key, name it whatever you want. Just make sure the field has the same name.

huangapple
  • 本文由 发表于 2023年7月6日 15:57:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定