java - 动态读取 Hibernate 配置

标签 java hibernate amazon-kms

我需要从另一个服务 AMS(就像亚马逊 KMS)获取 hibernate 连接 URL、用户名和密码配置。

我编写了另一种方法来从 AMS 获取这些值。但是如何设置/使用这些值 hibernate 来连接我的数据库。

例如。 hibernate.cfg.xml

<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>

Util.java

getAMSValue(propertyName){
...
}

我怎样才能实现这个目标?

最佳答案

为什么不在 java 文件中而不是 XML 中指定完整的配置?

查看此documentation

A org.hibernate.cfg.Configuration also allows you to specify configuration properties.

... ...

This is not the only way to pass configuration properties to Hibernate. Some alternative options include:

  1. Pass an instance of java.util.Properties to Configuration.setProperties().
  2. Place a file named hibernate.properties in a root directory of the classpath.
  3. Set System properties using java -Dproperty=value.
  4. Include elements in hibernate.cfg.xml (this is discussed later).
<小时/>

就您而言,您可以在 java 文件中进行类似的配置

Configuration configuration = new Configuration().configure();

...
...
configuration.setProperty("hibernate.connection.url", getAMSValue("url"));
configuration.setProperty("hibernate.connection.username", getAMSValue("username"));
configuration.setProperty("hibernate.connection.password", getAMSValue("password"));

关于java - 动态读取 Hibernate 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43132745/

相关文章:

multithreading - 两个并发请求,相同的 JPA 实体 - 有问题吗?

java - 如何设置请求 header (x-amz-服务器端加密 : aws:kms) while saving file to S3 in Java code?

java - 如何在不使用数据库中的任何角色表的情况下编写 spring security?

java - Hibernate:使用单例 session 写入日志(无刷新)

java - JTable AutoCreateSorter 排序不正确?

java - 我们可以使用 Post 方法从数据库中获取数据还是只使用 GET?

java - @Component Hibernate 类

mysql - 使用hibernate在查询MySQL数据库中排序

amazon-web-services - AWS SAM : How to create an S3 bucket with an already existing encryption key using SAM

.net - 使用 KMS 加密为 S3 对象生成预签名 URL - 我做错了什么?