使用外部属性文件配置的表名 [英] Table name configured with external properties file

查看:26
本文介绍了使用外部属性文件配置的表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个访问数据库并从中提取数据的 Spring-Boot 应用程序.一切正常,但我想从外部 .properties 文件配置表名.

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.

喜欢:

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

我试图找到一些东西,但我没有.

I tried to find something but I didn't.

您可以使用 @Value("...") 注释访问外部属性.

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

所以我的问题是:有什么办法可以配置表名吗?或者我可以更改/拦截hibernate发送的查询吗?

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?

解决方案:

好的,hibernate 5 与 PhysicalNamingStrategy 一起工作.所以我创建了自己的PhysicalNamingStrategy.

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);
        }
    }
}
}

此外,这篇 关于 NamingStrategy 的 Stackoverflow 文章给了我这个想法.

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

推荐答案

表名实际上是通过其策略接口来自 hibernate 本身.Boot 将其配置为 SpringNamingStrategy 并且在 Boot 2.x 中进行了一些自定义更改.值得一读 gh-1525 进行这些更改的地方.配置Hibernate 命名策略有更多信息.

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.

有一些想法可以添加一些自定义属性来配置 SpringNamingStrategy,但我们允许更轻松地自定义整个策略 bean,因为这允许用户执行他们需要做的任何事情.

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,没有像您所问的那样直接进行配置,但我假设如果您创建自己的策略,那么您可以将自己的属性自动连接到那里.在那些自定义的策略界面中,您将看到实体名称,您可以在引导的配置属性中为此保留一个键空间并匹配实体名称.

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

您的配置属性将采用 mytables,并且在该 naming 中将包含一个 Map.然后在您的自定义策略中,只需从映射表中检查您是否定义了自定义名称.

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.

这篇关于使用外部属性文件配置的表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆