我构建了一个Spring-Boot应用程序,用于访问数据库并从中提取数据。 一切工作正常,但我想从外部.properties文件配置表名称。
喜欢:
@Entity @Table(name = "${fleet.table.name}") public class Fleet { ... }我试图找到一些东西,但我没有。
您可以使用@Value("...")注释访问外部属性。
所以我的问题是:有什么办法可以配置表名? 或者我可以更改/拦截由休眠发送的查询吗?
解:
好吧,休眠5与PhysicalNamingStrategy 。 所以我创建了自己的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文章给了我这个想法。
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.
最满意答案
表名实际上是通过其策略接口从hibernate本身获得的。 Boot将其配置为SpringNamingStrategy并且Boot 2.x中有一些更改可以自定义。 值得阅读gh-1525这些变化。 配置Hibernate命名策略有更多信息。
有一些想法可以添加一些自定义属性来配置SpringNamingStrategy但是我们允许更轻松地定制整个策略bean,因为它允许用户SpringNamingStrategy地执行任何操作。
AFAIK,没有直接的方式来配置你想要的配置,但我认为如果你创建自己的策略,你可以自动连线你自己的属性到那里。 就像那些自定义的策略界面一样,您将看到实体名称,您可以在引导的配置属性中为此保留一个密钥空间并匹配实体名称。
mytables.naming.fleet.name=foobar mytables.naming.othertable.name=xxx你的配置属性需要mytables并且在这个naming中将是一个Map 。 然后,在您的自定义策略中,只需通过从映射表中检查是否定义了自定义名称即可。
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=xxxYour 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.
更多推荐
发布评论