英文:
Spring Data JDBC Custom Naming Strategy
问题
有没有办法更改引用实体的默认列命名策略,而不是使用默认的(类的名称)。文档说要在RelationalMappingContext
的命名策略上调用setForeignKeyNaming(ForeignKeyNaming.IGNORE_RENAMING)
方法。但是,该方法仅在实现DefaultNamingStrategy
和JdbcMappingContext
的命名策略上可用,但它是包私有的(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论