使用into persistence.xml JPA属性读取环境变量

时间:2016-06-03 19:15:29

标签: json jpa ibm-cloud openjpa persistence.xml

我在Bluemix中有一个用户定义的服务(不在Bluemix云上的内部部署数据库),凭据如下所示:

System-Provided:
{
 "VCAP_SERVICES": {
  "user-provided": [
   {
    "credentials": {
     "dbname": "",
     "host": "",
     "password": "",
     "port": "",
     "username": ""
    },
    "label": "user-provided",
    "name": "some-service",
    "syslog_drain_url": "",
    "tags": []
   }
  ]
 }
}

有没有办法以某种方式解析JSON并将其读入这些持久性单元属性?

<properties>
  <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
  <property name="javax.persistence.jdbc.url" value=''/>
  <property name="javax.persistence.jdbc.user" value=""/>
  <property name="javax.persistence.jdbc.password" value=""/>
</properties>

1 个答案:

答案 0 :(得分:0)

创建EntityManagerFactory时,您可以传递自定义值来覆盖这样的持久性文件:

HashMap<String,String> props=new HashMap<>();
props.put("javax.persistence.jdbc.url", "yourUserName");
EntityManagerFactory emf=Persistence.createEntityManagerFactory("YourPersistenceUnitPU",props);

要解析JSON,您可以使用google gson lib。