1

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

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
vancan1ty
  • 563
  • 1
  • 4
  • 16
  • Does it work with XPU ? – wypieprz Apr 28 '14 at 21:03
  • Yes. Additionally if I swap the names of the persistence units, the unit which now works breaks and the other one works. It occurs to me that perhaps two persistence units are not allowed to share the same data source. Or perhaps there are more configuration files I need to change to add a second persistence unit? – vancan1ty Apr 28 '14 at 22:30
  • Is anyone aware of a source for comprehensive documentation for this sort of thing? – vancan1ty Apr 28 '14 at 22:30
  • This should work, please add your `pom.xml` and your connection-pool-configuration to the question. – unwichtich Apr 29 '14 at 18:22

0 Answers0