Home » Java » java – How to set schema name dynamically in JPA-Exceptionshub

java – How to set schema name dynamically in JPA-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m working with an application which using Java+EJB+JPA technologies. There is a new change came related to database that Schema name should be based on the environment on which application is running. Actually this is a special type of UAT environment (Clone of existing UAT) we configured so both environment will use UAT database but different schema. Here is my current persistence.xml:

<persistence-unit name="db2_mydb" transaction-type="JTA">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <jta-data-source>java:app/env/jdbc/myapp</jta-data-source>
    <properties>
                <property name="openjpa.jdbc.DBDictionary" value="JoinSyntax=sql92, DisableAlterSeqenceIncrementBy=true" />
                <property name="openjpa.jdbc.Schema" value="SCHEMA_X" />
                <property name="openjpa.Log" value="log4j" />
                <property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72, PrintParameters=true" />
                <property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
                <property name="openjpa.ConnectionRetainMode" value="transaction" />
                <property name="openjpa.jdbc.DBDictionary" value="JoinSyntax=sql92" />
                <property name="openjpa.jdbc.TransactionIsolation" value="read-committed"/>
                <property name="openjpa.DetachState" value="all(DetachedStateManager=true)"/>
    </properties>
</persistence-unit>

I’m injecting entitymanager using the persistance unit name db2_mydb in Data layer class. In Java I have the environment information whether X or Y Based on which I have to use Schema SCHEMA_X or SCHEMA_Y . How Can I achieve this. I’m not getting any idea to make it dynamic. I’m using OpenJPA as implementation.

How to&Answers: