0

I have created an app that uses JPA as well as servlets with JAX-RS.

When I run the app locally, I am able to query my server with curl and get the proper response for all the GET and POST requests. However, when I host the server on IBM Cloud and I query it with curl I get the error: "No Persistence provider for EntityManager".

What's causing the discrepancy between the local and remote (IBM Cloud) environments and what can I do to fix it?


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="ibmcloud">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <non-jta-data-source>java:comp/DefaultDataSource</non-jta-data-source>
        <class>entities.Comment</class>
        <properties>
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.use_sql_comments" value="true"/>

        </properties>
    </persistence-unit>
</persistence>

.travis.yml

language: java
jdk: oraclejdk8
sudo: false
before_install:
   mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
before_deply:
   cf login -u $BLUEMIX_USER -o $BLUEMIX_ORG
script:
   mvn test -B
language: java
git:
  depth: 1
dist: trusty
cache:
  directories:
  - "$HOME/.m2"
deploy:
  edge: true
  provider: bluemixcloudfoundry
  username: $BLUEMIX_USER
  password: $BLUEMIX_PASSWORD
  organization: $BLUEMIX_ORG
  space: $BLUEMIX_SPACE
  manifest: manifest.yml
  app_name: ibmcloudapp
  region: eu-gb
  api: https://api.eu-gb.bluemix.net
  skip_cleanup: true

manifest.yml

applications:
 - name: ibmcloud
   path: target/ibmcloud.war
   instances: 1
   random-route: true

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dotheminimum</groupId>
    <artifactId>ibmcloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1.1</version>
</dependency>
        <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<!-- <dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.2.Final</version>
</dependency> -->
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>       
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
        </plugins>
    </build>
</project>

The source code for the app is hosted here

edoreld
  • 303
  • 1
  • 17
  • You can type that message into the search bar, https://stackoverflow.com/search?q=No+Persistence+provider+for+EntityManager –  Mar 02 '19 at 15:12
  • Yes, and if I search with tag [ibm-cloud] it shows one results that corresponds to this question. – edoreld Mar 02 '19 at 15:14
  • 1
    Can you actually check is there org.hibernate.jpa.HibernatePersistenceProvider on classpath and can you find it through source code or somewhere else? HibernatePersistenceProvider or HibernatePersistence should be a part of hibernate package. Maybe it works locally, beause of your local maven repository already have artifacts like persistence and others. – cyberra Mar 02 '19 at 15:59
  • 1
    Yes and when you search ALL of those issues they all come back to the same thing. User either has an incorrect `jpa-api` jar, or doesn't have all required jars of the JPA provider, or an incorrectly configured `persistence.xml`. That's all there is. The only thing different between your two deployments is the environment. The JPA persistence code is NOT the issue in any way shape or form. It's the jars –  Mar 02 '19 at 16:37

1 Answers1

1

The problem was that my "persistence.xml" file was under "src/main/java/META-INF" instead of "src/main/resources/META-INF".

My local environment was able to find the file, but IBM Cloud didn't. After changing the path of "persistence.xml", the file was detected by IBM Cloud.

If I had been exhaustive, I would have been able to find my solution in the comments of the first result of the search linked to by Billy Frost (No Persistence provider for EntityManager named)

edoreld
  • 303
  • 1
  • 17