在没有persistence .xml的情况下,是否有其他方法可以为Hibernate响应式提供用户和密码?



我正试图找出一种方法来提供用户和密码,而无需在同一服务器上硬编码它们。我需要通过api来获取它们。


<!-- MySQL -->
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/mydatabase"/>
<!-- Credentials -->
<property name="javax.persistence.jdbc.user"
value="this is user"/>
<property name="javax.persistence.jdbc.password"
value="this is password"/>

上面是如何做到现在,但我想把这些,所以谁有代码访问仍然不能看到用户和密码。我怎样才能做到呢?我在这个项目中没有JPA。

我不确定您的需求是什么,但一个非常灵活的选择是实现RectiveConnectionPool

https://hibernate.org/reactive/documentation/1.1/reference/html_single/_custom_connection_management_and_multitenancy

您可以使用javax.persistence.Persistence的方法:

/**
* Create and return an EntityManagerFactory for the named persistence unit
* using the given properties.
* 
* @param persistenceUnitName
*            the name of the persistence unit
* @param properties
*            Additional properties to use when creating the factory. 
*            These properties may include properties to control
*            schema generation.  The values of these properties override 
*            any values that may have been configured elsewhere.
* @return the factory that creates EntityManagers configured according to
*         the specified persistence unit.
*/
public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {

从API获取用户名和密码,并将它们存储在Map中,如下所示:

var props = new HashMap<String, String>();
fetch("javax.persistence.jdbc.url", props);
fetch("javax.persistence.jdbc.password", props);
fetch("javax.persistence.jdbc.user", props);

然后调用:

emf = Persistence
.createEntityManagerFactory("hibernate-config", props)

相关内容

  • 没有找到相关文章

最新更新