Search code examples
springconfigurationspring-dataspring-data-jdbc

Spring Data JDBC Custom Naming Strategy


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?


Solution

  • @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.