0

In an ear-file I have a persistence-unit for managing entities inside that ear which works fine. Additionally, there is a commons jar-file located in APP-INF/lib which is a dependency in other ears as well (other ear-modules on the same level). In this commons-jar I would like to have a single entity and create the possibility for CRUD on that entity. Now it is a jar only with an additional persistence unit (but same db as for the ear) and I can't manage to set it all up. Would that persistence unit be managed as well by the container? Could I use EJBs inside that jar file as well? Is it possible at all to do what I want?

I created the entity as follows

@Entity
public class ConfigEntity {

  @Id
  private Long id;
  @Version
  private Long version;

  [further fields, getters and setters omitted]

}

With the following "Dao" I try to call isEntityManagerNull from a bean inside the ear.

public class ConfigBEDao {

  @PersistenceContext(unitName = "configurationPU")
  EntityManager entityManager;

  public boolean isEntityManagerNull() {
    return entityManager == null;
  }

}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="configurationPU" transaction-type="RESOURCE_LOCAL">
        <description/>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/db</jta-data-source>
        <properties>
            <property name="eclipselink.target-database" value="${database.dialect}"/>
            <property name="eclipselink.logging.level" value="FINE"/>
            <property name="eclipselink.logging.parameters" value="true"/>
            <property name="eclipselink.cache.shared.default" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

From a bean inside the ear file I call

ConfigBEDao dao = new ConfigBEDao();
dao.isEntityManagerNull();

which gives me the following exception already when trying to deploy on the server (payara):

java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [ConfigBEDao/entityManager] in the scope of the module called [main_ear-1.0-SNAPSHOT#main_ejb-1.0-SNAPSHOT.jar]. Please verify your application.
    at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:733)
    at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.findReferencedPUs(EjbBundleDescriptorImpl.java:889)
    at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:186)
    at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)

Then I changed the persistence.xml as follows but that didn't work either although the application can be deployed:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="configurationPU" transaction-type="RESOURCE_LOCAL">
        <description/>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/db"/>
            <property name="javax.persistence.jdbc.user" value="db"/>
            <property name="javax.persistence.jdbc.password" value="secret"/>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

which gave me

javax.persistence.PersistenceException: No Persistence provider for EntityManager named configurationPU
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
Klave
  • 23
  • 5
  • You might want to try a simple tutorial and get it working before making complex changes. For instance, you set it to be transaction-type="RESOURCE_LOCAL", but then tell it to use a JTA datasource - a persistence unit uses JTA or it doesn't. Second, how is "${database.dialect}" going to get resolved in your persistence unit? The error you are getting though seems like the persistence unit isn't being loaded (as it would show other errors with the persistence.xml file mentioned above) see http://stackoverflow.com/a/34054304/496099 – Chris Apr 24 '17 at 19:47
  • Ok, thanks. Will try to do that and then come back. – Klave Apr 25 '17 at 06:07

0 Answers0