OpenJPA Schema Configuration

I am using OpenJPA 1.2.3 in WebSphere with DB2. Is there a way to create and bundle my application so that the same application (EAR) can have a changing schema name based on the environment (DEV, ACPT, PROD, etc.).

My PU is configured to manage the container 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 reviewed this in ORM.xml, but it is still a static value for the schema and does not externalize the setting; In addition, it does not work (I saw how many topics discussed this). I also considered including this configuration in a WebSphere data source; this doesn't work either.

- Keith

+3
source share
4 answers

OpenJPA, , openjpa.jdbc.Schema . (.. jdbc/DataSource) .

?

+3

maven.

1- pom maven.

    <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- persistence.xml :

    <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 pom.

    <build>        
        <resources>
            <resource>
                <directory>src/main/resources/META-INF</directory>
                <filtering>true</filtering>
                <targetPath>/META-INF</targetPath>
            </resource>
        </resources>
    </build>
+2

<persistence-unit>, , .
:

<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>
0

:

@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;
}
0

Source: https://habr.com/ru/post/1785541/


All Articles