I am trying to set up two persistence units in my app to map to different schemas in the underlying postgresql database. I am running the app with glassfish 4.
/web/META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="XPU" transaction-type="JTA">
<jta-data-source>postother2</jta-data-source>
<mapping-file>Xmapping.xml</mapping-file>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
<persistence-unit name="YPU" transaction-type="JTA">
<jta-data-source>postother2</jta-data-source>
<mapping-file>Ymapping.xml</mapping-file>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
The mapping-files contain the schema which jpa is supposed to use. They are in the root of my source directory and look like this:
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<schema>java_app</schema>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
I am attempting to specify to use YPU in my ejb like so:
@Stateless
@Path("datajson")
public class DataJsonFacadeREST extends AbstractFacade<DataJson> {
@PersistenceContext(unitName = "YPU")
private EntityManager em;
AbstractFacade is a Netbeans autogenerated class which implements some common CRUD operations on an entity.
When I attempt to run, I get the below error.
SEVERE: java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [net.company.service.DataJsonFacadeREST/em] in the scope of the module called [Splash2]. Please verify your application.
Any advice as to what I could be doing wrong? The app worked perfectly as long as I was only using one schema (and only had one entry in persistence.xml).