0

I am trying to use EclipseLink and JPA 2.1.0 to try connecting to the DB. However, the application is not able to spot/recognize the persistence.xml placed in the folder. Please can you help?

pom.xml

    <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.0</version>
    </dependency>
    <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.0</version>
    </dependency>

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="Employee" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.sap.cloud.sdk.model.model.Employee</class>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        </properties>
    </persistence-unit>
</persistence>

The above persistence.xml is placed in the folder structure:

firstapp
 - application
 - src
    - main
       - java
         - package
       - resources
         - META-INF
            - persistence.xml
       - Webapp
         - WEB-INF
            - web.xml

The model is as shown below:

@Entity
@Table(name = "EMPLOYEE")
@Multitenant(value=MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type=TenantTableDiscriminatorType.SCHEMA, contextProperty="eclipselink-tenant.id")
@NamedQueries({ @NamedQuery(name = "Employees", query = "SELECT c FROM Employee c")})
public class Employee implements Serializable {
    /**
     * The (globally unique) ID of the object.
     */
    @Id
    @Column(name="GUID", length = 36)
    private String guid = UUID.randomUUID().toString();

    private static final long serialVersionUID = 1L;


    @Column(name = "NAME", length = 36, nullable = true)
    String name = null;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGuid()
    {
        return this.guid;
    }

    public void setGuid(String guid)
    {
        this.guid = guid;
    }
}

I am trying to use the above named query:

@PersistenceContext(unitName = "Employee")
    private EntityManager em;

    @Override
    protected void doGet( final HttpServletRequest request, final HttpServletResponse response )
        throws IOException
    {
        logger.info("I am running!");
//        response.getWriter().write("Hello World.. This is a sample application!");
        String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
        String title = "Sample Code for multitenancy Test";

        String tenantId = getTenantId();
        Map<String, String> props = new HashMap<String, String>();
        props.put("elipselink.tenant.id", tenantId);
        em = this.getEntityManagerFactory().createEntityManager(props);
        Query query = em.createNamedQuery("Employees");
        List<Employee> retVal = query.getResultList();

In the integration test, I get the error that its not able to spot the persistence unit mentioned in the Entity manager annotation.

SEVERE: FAIL ... 86e424ac-8560-4b85-9df3-4c7253559d80:  Missing required persistence.xml for @PersistenceContext ref "em" to unit "Employee"
Aug 26, 2018 8:33:40 AM org.apache.openejb.config.ReportValidationResults logResults
SEVERE: Invalid EjbModule(name=86e424ac-8560-4b85-9df3-4c7253559d80, path=/Users/i048564/Documents/Eclipse/neon/firstapp/integration-tests/target/arquillian-working-dir/1/a
rquillian-tomee-app-working-dir/0/86e424ac-8560-4b85-9df3-4c7253559d80)

I have tried to place the persistence.xml in the webapp folder as well, as mentioned in the other posts. But, it doesnt help.

  • You've shown a directory structure that looks to be from an IDE project, not the final deployment archive. What matters is the META-INF directory is placed in the same directory as the compiled package class files for the deployment or where it is run for testing. Can you show details on how the archive is built and looks like before it is deployed, or what you are using to build/deploy/test? Also check that your pom is respected and the EclipseLink classes are actually downloaded and on the class path of your environment. – Chris Aug 28 '18 at 14:39

2 Answers2

0

(disclaimer : need to write below post as comment, but I can not do it under this situation. I just want to give a help hand, because myself struggled with this JPA at a time.)

This Link shows how to user org.eclipse.persistence.jpa.PersistenceProvider provide, with emf (EntityManageFactory). As per my understanding if RESOURCE_LOCAL transaction type need to be used with @PersistenceUnit. This description clarify it.

Otherwise please check with this. Change provider as org.apache.openjpa.persistence.PersistenceProviderImpl. The path of persistence.xml is correct.

GrootC
  • 113
  • 2
  • 10
0

You used a resource local unit so you likely want to inject an EntityManagerFactory using @PersistenceUnit and create your entity manager yourself. If not maybe move to a JTA unit.

Romain Manni-Bucau
  • 3,354
  • 1
  • 16
  • 13