不能在persistence.xml中读取环境变量 [英] Not able to read environment variable in persistence.xml

查看:225
本文介绍了不能在persistence.xml中读取环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven3项目,我在使用tomcat7-maven-plugin。我想通过环境变量参数为jvm设置嵌入式数据库的路径。



使用System.getenv(myDataDir)在Java中读取变量 - 方法返回正确的路径。
但是当我尝试在我的persistence.xml中设置变量$ {myDataDir},然后我使用mvn tomcat:run启动tomcat时,我得到FileNotFoundExceptions,因为变量不会被替换为实际值(例如找不到$ {myDataDir} \derby.log的路径)



我不知道是什么原因造成的 - 如果是不持续性提供者(EclipseLink)支持这个或者是别的东西。



我的persistence.xml如下所示:

 code>< persistence xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://java.sun.com/xml/ns/persistence http ://java.sun.com/xml/ns/persistence/persistence_2_0.xsdversion =2.0xmlns =http://java.sun.com/xml/ns/persistence> 
< persistence-unit name =myPersistencetransaction-type =RESOURCE_LOCAL>
< provider> org.eclipse.persistence.jpa.PersistenceProvider< / provider>
<属性>
< property name =javax.persistence.jdbc.drivervalue =org.apache.derby.jdbc.EmbeddedDriver/>
< property name =javax.persistence.jdbc.urlvalue =jdbc:derby:$ {myDataDir} / DB; create = true; upgrade = true/>
< property name =javax.persistence.jdbc.uservalue =admin/>
< property name =javax.persistence.jdbc.passwordvalue =password/>
< property name =eclipselink.ddl-generationvalue =create-tables/>
< property name =eclipselink.ddl-generation.output-modevalue =both/>
< property name =eclipselink.logging.levelvalue =SEVERE/>
< property name =eclipselink.logging.filevalue =$ {myDataDir} /derby.log/>
< property name =eclipselink.application-locationvalue =$ {myDataDir} / dbScripts/>
< / properties>
< / persistence-unit>
< / persistence>



编辑:



我忘了提,我在一个Spring 3框架环境。根据网络上的示例,这应该能够在persistence.xml中使用环境变量...

解决方案

使用$数据库URL中的{myDataDir}无效,除非您的数据库支持,或者除非您在自己的构建脚本中翻译变量。



您可以做的是将属性映射传递给具有所需URL的Persistence.createEntityManagerFactory(),您必须在运行时在代码中构建。


I have a Maven3 project where I'm using the tomcat7-maven-plugin. I would like to set the path for the embedded database via an environment variable argument to the jvm.

Reading the variable with System.getenv("myDataDir") within a Java-Method returns the correct path. But when I try to set the variable ${myDataDir} in my persistence.xml and then I start tomcat with "mvn tomcat:run" I get FileNotFoundExceptions because the variable is not replaced with the actual value (it says e.g. Cannot find path for ${myDataDir}\derby.log)

I don't know what's causing this - if it's the persistence provider (EclipseLink) that doesn't support this or if it's something else.

My persistence.xml looks like this:

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="myPersistence" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
      <property name="javax.persistence.jdbc.url" value="jdbc:derby:${myDataDir}/DB;create=true;upgrade=true" />
      <property name="javax.persistence.jdbc.user" value="admin" />
      <property name="javax.persistence.jdbc.password" value="password" />
      <property name="eclipselink.ddl-generation" value="create-tables" />
      <property name="eclipselink.ddl-generation.output-mode" value="both" />
      <property name="eclipselink.logging.level" value="SEVERE" />
      <property name="eclipselink.logging.file" value="${myDataDir}/derby.log" />
      <property name="eclipselink.application-location" value="${myDataDir}/dbScripts" />
    </properties>
  </persistence-unit>
</persistence>

EDIT:

I forgot to mention, that I'm in a Spring 3 Framework environment. According to the examples on the web, this should be capable of using environment variables in persistence.xml...

解决方案

Using ${myDataDir} in a database URL is not valid, unless supported by your database, or unless you are translating the variable during your own build scripts.

What you can do is pass a properties map to Persistence.createEntityManagerFactory() that has the URL you want, that you must build at runtime in code.

这篇关于不能在persistence.xml中读取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆