OpenJPA schema configuration
I am using OpenJPA 1.2.3 on WebSphere with DB2. Is there a way to build and bundle my application in a way that allows for the same application (EAR) to have a changing Schema name based on environment (DEV, ACPT, PROD, etc).
My PU is setup 开发者_如何学Goup to be container managed as follows:
<persistence>
<persistence-unit name="My_PU" transaction-type="JTA">
<jta-data-source>jdbc/DataSource</jta-data-source>
...
<properties>
<property name="openjpa.jdbc.Schema" value="MYSCHEMA"/>
<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
</properties>
</persistence-unit>
</persistence>
I have looked into putting this in the ORM.xml, but this is still a static value for the schema, and does not externalize the setting; also, it doesn't seem to work (I have seen the many threads discussing this). I also, have looked into putting this configuration into the WebSphere data source; this does not seem to work either.
--Keith
I'm not familiar with OpenJPA, but I'm guessing that the openjpa.jdbc.Schema
property is optional. The datasource (i.e. the one at jdbc/DataSource
) will dictate the default schema in use.
Have you tried leaving the property out?
You can use maven.
1-Define profiles for each build scenario in the project pom file or maven settings.
<profiles>
<profile>
<id>DEV Profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<propertis>
<db-schema>DEVSCHEMA</db-schema>
</propertis>
</profile>
<profile>
<id>PROD Profile</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<propertis>
<db-schema>PRODSCHEMA</db-schema>
</propertis>
</profile>
</profiles>
2- change your persistence.xml as follows:
<persistence>
<persistence-unit name="persistent-unit" transaction-type="JTA">
<jta-data-source>jdbc/DataSource</jta-data-source>
...
<properties>
<property name="openjpa.jdbc.Schema" value="${db-schema}"/>
<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
</properties>
</persistence-unit>
</persistence>
3- Put following lines in the project pom file.
<build>
<resources>
<resource>
<directory>src/main/resources/META-INF</directory>
<filtering>true</filtering>
<targetPath>/META-INF</targetPath>
</resource>
</resources>
</build>
I just create separate <persistence-unit>
elements and then dynamically select which one to bind to.
For example:
<persistence>
<persistence-unit name="DEV_PU" transaction-type="JTA">
<jta-data-source>jdbc/DataSource</jta-data-source>
...
<properties>
<property name="openjpa.jdbc.Schema" value="DEVSCHEMA"/>
<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
</properties>
</persistence-unit>
<persistence-unit name="PROD_PU" transaction-type="JTA">
<jta-data-source>jdbc/DataSource</jta-data-source>
...
<properties>
<property name="openjpa.jdbc.Schema" value="PRODSCHEMA"/>
<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
</properties>
</persistence-unit>
</persistence>
You can set schema name dynamically in the code like so:
@Entity
@Table(name = "ITEM", schema=Item.SCHEMA)
public class Item implements Serializable {
public static final String SCHEMA = System.getProperty("env.schema");
@Id
@Column (name = "ITEM_ID")
private String id;
@Column (name = "ITEM_NAME")
private String name;
}
精彩评论