2011-05-04 92 views
3

我們正在拆分我們的基準(用於1.0版和2.0版的併發開發)。我們正在研究其他替代方案,而不是維護兩個單獨的數據庫(和硬件)。我們希望能夠使用一個數據庫的同一個實例,並具有表/數據的重複副本以兩種不同的模式:將屬性注入到JPA orm.xml中?

1.0:SCHEMA_1
2.0:SCHEMA_2

的JPA orm.xml中文件具有在指定了模式中的特性:

<schema>SCHEMA_1</schema> 

我的問題是一個屬性是否可以注射的,而不是硬編碼的模式名(以及如何)。

例如,如果我們有.properties文件具有以下內容:

<SCHEMA>schema.name</SCHEMA> 

感謝:

schema.name=SCHEMA_1 

我們才能在orm.xml文件像這樣使用schema.name任何幫助!使圖式動態化的其他選擇也是受歡迎的。

回答

2

我們結束了使用速度,Maven的插件,以取代在編譯時的模式標籤。

orm.xml中的模板:

<schema>${schemaName}</schema> 

在POM文件:

 <plugin> 
     <groupId>net.rumati.maven.plugins</groupId> 
     <artifactId>velocity-maven-plugin</artifactId> 
     <version>0.1.1</version> 
     <executions> 
      <execution> 
       <id>replaceSchema</id> 
       <phase>generate-sources</phase> 
       <goals> 
       <goal>velocity</goal> 
       </goals> 
      </execution> 
     </executions> 
     <configuration> 
      <template>${basedir}/src/main/resources/hibernate/template/orm.xml</template> 
      <properties> 
      <schemaName>${r1Schema}</schemaName> 
      </properties> 
      <outputFile>${basedir}/src/main/resources/hibernate/orm.xml</outputFile> 
     </configuration> 
    </plugin> 
3

JPA只在orm.xml中定義了這個。

在EclipseLink的,你可以使用一個SessionCustomizer在代碼中設置這對你的EclipseLink會話,

session.getLogin().setTableQualifier("SCHEMA_1"); 
+0

感謝您的建議。 – Nathan 2011-05-06 18:15:15

2

我一直面臨着確切的問題。不要將orm.xml構建到項目中,而應將其置於工件構建之外。然後在運行時,把它放在應用程序的類路徑中,所以它拾起:

<persistence-unit-metadata> 
    <persistence-unit-defaults> 
     <schema>MySchemaThatsBeenDeterminedAtRuntime</schema> 
    </persistence-unit-defaults> 
</persistence-unit-metadata> 

應用程序的每個實例都可以有它自己的orm.xml中並指向一個不同的模式。