JPA using multiple database schemas

I had the same problem I solved that with a persistence.xml in which I refer to the needed orm.xml files within I declared the db shema

<persistence
xmlns="http://java.sun.com/xml/ns/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" >
<persistence-unit name="schemaOne">
    . . .
    <mapping-file>ormOne.xml</mapping-file>
    . . .
</persistence-unit>

<persistence-unit name="schemaTwo">
    . . .
    <mapping-file>ormTwo.xml</mapping-file>
    . . .
 </persistence-unit>
</persistence>

now you can create a EntityManagerFactory for your special schema

EntityManagerFactory emf = Persistence.createEntityManagerFactory("schemaOne");

Leave a Comment