Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table name configured with external properties file

I build a Spring-Boot application that accesses a Database and extracts data from it. Everything is working fine, but I want to configure the table names from an external .properties file.

like:

@Entity
@Table(name = "${fleet.table.name}")
public class Fleet {
...
}

I tried to find something but I didn't.

You can access external properties with the @Value("...") annotation.

So my question is: Is there any way I can configure the table names? Or can I change/intercept the query that is sent by hibernate?

Solution:

Ok, hibernate 5 works with the PhysicalNamingStrategy. So I created my own PhysicalNamingStrategy.

@Configuration 
public class TableNameConfig{

    @Value("${fleet.table.name}")
    private String fleetTableName;

    @Value("${visits.table.name}")
    private String visitsTableName;

    @Value("${route.table.name}")
    private String routeTableName;

    @Bean
    public PhysicalNamingStrategyStandardImpl physicalNamingStrategyStandard(){
        return new PhysicalNamingImpl();
    }

class PhysicalNamingImpl extends PhysicalNamingStrategyStandardImpl {

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        switch (name.getText()) {
            case "Fleet":
                return new Identifier(fleetTableName, name.isQuoted());
            case "Visits":
                return new Identifier(visitsTableName, name.isQuoted());
            case "Result":
                return new Identifier(routeTableName, name.isQuoted());
            default:
                return super.toPhysicalTableName(name, context);
        }
    }
}
}

Also, this Stackoverflow article over NamingStrategy gave me the idea.

like image 404
DK1990 Avatar asked Mar 07 '18 09:03

DK1990


People also ask

How do you externalize application properties?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.

What does ${ mean in a properties file?

The properties files are processed in the order in which they appear on the command line. Each properties file can refer to properties that have already been defined by a previously processed properties file, using ${varname} syntax.

What is external configuration in spring boot?

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.


1 Answers

Table names are really coming from hibernate itself via its strategy interfaces. Boot configures this as SpringNamingStrategy and there were some changes in Boot 2.x how things can be customised. Worth to read gh-1525 where these changes were made. Configure Hibernate Naming Strategy has some more info.

There were some ideas to add some custom properties to configure SpringNamingStrategy but we went with allowing easier customisation of a whole strategy beans as that allows users to to whatever they need to do.

AFAIK, there's no direct way to do config like you asked but I'd assume that if you create your own strategy you can then auto-wire you own properties to there. As in those customised strategy interfaces you will see the entity name, you could reserve a keyspace in boot's configuration properties to this and match entity names.

mytables.naming.fleet.name=foobar
mytables.naming.othertable.name=xxx

Your configuration properties would take mytables and within that naming would be a Map. Then in your custom strategy it would simply be by checking from mapping table if you defined a custom name.

like image 83
Janne Valkealahti Avatar answered Oct 18 '22 23:10

Janne Valkealahti