简体   繁体   English

Spring boot jpa 来自属性文件的实体表名

[英]Spring boot jpa entity table name from property file

We are working on a spring boot library to generate and validate OTP.我们正在开发一个 spring 启动库来生成和验证 OTP。 It uses database to store the OTP.它使用数据库来存储 OTP。

We are using Spring Data JPA for Database operations, as it will be easy to handle multiple database systems according to the project.我们使用 Spring 数据 JPA 进行数据库操作,因为根据项目处理多个数据库系统会很容易。

Now we have ran in to a problem, most of our projects uses Oracle with a single database.现在我们遇到了一个问题,我们的大多数项目都使用 Oracle 和单个数据库。 When using the the same lib in multiple projects there is a name conflict.在多个项目中使用相同的库时会出现名称冲突。 So we want the name of the OTP table to be configurable using a property file.所以我们希望 OTP 表的名称可以使用属性文件进行配置。

We tried @Table(name = "${otp-table-name}") But its not working.我们尝试@Table(name = "${otp-table-name}")但它不起作用。

We did a lots of research and found out the hibernate naming strategy configuration can help.我们做了很多研究,发现 hibernate 命名策略配置可以提供帮助。

But we dont want to use lots of configuration in our library as we need the library to be easily usable in the projects.但是我们不想在我们的库中使用大量配置,因为我们需要该库在项目中易于使用。

Can someone help us on this aspect.有人可以在这方面帮助我们吗?

Thanks in advance.提前致谢。

You can dynamically determine the actual DataSource based on the current context, use Spring's AbstractRoutingDataSource class. You could write your own version of this class and configure it to use a different data source based on the property file.您可以根据当前上下文动态确定实际数据源,使用 Spring 的AbstractRoutingDataSource class。您可以编写自己的版本 class,并根据属性文件将其配置为使用不同的数据源。

This allows you to switch between databases or schema without having to change the code in your library.这允许您在数据库或模式之间切换,而无需更改库中的代码。

See: https://www.baeldung.com/spring-abstract-routing-data-source参见: https://www.baeldung.com/spring-abstract-routing-data-source

Using a NamingStrategy is good approach.使用NamingStrategy是一种好方法。

You could let it delegate to an existing NamingStrategy and add a prefix.您可以让它委托给现有的NamingStrategy并添加前缀。

Use a library specific default for the prefix, but also allow users of your library specify an alternative prefix.使用特定于图书馆的默认前缀,但也允许图书馆的用户指定替代前缀。

This way your library can be used without extra configuration, but can also handle the case of multiple applications using it in the same database schema.这样你的库就可以在没有额外配置的情况下使用,但也可以处理多个应用程序在同一个数据库模式中使用它的情况。

Of course this might involve the risk of someone using the default prefix without realizing that, that is already used.当然,这可能涉及有人在没有意识到已经使用过的默认前缀的情况下使用的风险。 It is not clear what the consequences of that scenario are.目前尚不清楚这种情况的后果是什么。 If the consequences are really bad you should drop the default value and require that a project specific prefix is used.如果后果真的很糟糕,你应该放弃默认值并要求使用项目特定的前缀。 When no prefix is specified throw an exception with an instructional error message telling the user, ie the developer how to pick a prefix and where to put it.当没有指定前缀时,会抛出一个异常,并显示一条指示性错误消息,告诉用户,即开发人员如何选择前缀以及将前缀放在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM