0
votes

I'm getting this error:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named EmployeeDb at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) at staffManagement.test.TestHarness.main(TestHarness.java:14)

But I just can't understand why - I did the identical thing on my home pc and had no issues. Here is my code for my test file:

public class TestHarness {

public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmpDb");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    Employee employee1 = new Employee("Brad", "Pitt", "Actor", 10000);
    em.persist(employee1);

    tx.commit();
    em.close();
}

}

and my persistence.xml file:

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence">

  <persistence-unit name="EmpDb"   transaction-type="RESOURCE_LOCAL">

  <class>staffManagement.domain.Employee</class>

  <properties>                    
        <property name="javax.persistence.jdbc.driver"   value="org.apache.derby.jdbc.ClientDriver" />
        <property name="javax.persistence.jdbc.url"      value="jdbc:derby://localhost:1527/DbName" />

        <property name="javax.persistence.jdbc.user"     value="APP" />
        <property name="javax.persistence.jdbc.password" value="APP" />

        <property name="eclipselink.logging.level"              value="FINE" /> 
        <property name="eclipselink.ddl-generation"             value="create-tables" />
        <property name="eclipselink.ddl-generation.output-mode" value="database" />
   </properties>

Please help me! I'm ripping my hair out. I'm using Eclipse and Derby db. I have the persistence.xml file in the meta-inf folder.

3
See my updated answer.Alex K.

3 Answers

0
votes

You need to wrap everything in persistence.xml with persistence-unit tag.

    <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="EmpDb">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
      <class>staffManagement.domain.Employee</class>

      <properties>                    
            <property name="javax.persistence.jdbc.driver"   value="org.apache.derby.jdbc.ClientDriver" />
            <property name="javax.persistence.jdbc.url"      value="jdbc:derby://localhost:1527/DbName" />
            <property name="eclipselink.target-database" value="Derby"/>            


            <property name="javax.persistence.jdbc.user"     value="APP" />
            <property name="javax.persistence.jdbc.password" value="APP" />

            <property name="eclipselink.logging.level"              value="FINE" /> 
            <property name="eclipselink.ddl-generation"             value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
       </properties>
    </persistence-unit>

Also, specify persistence provider and target DB:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<property name="eclipselink.target-database" value="Derby"/>            
0
votes

You will need to move the persistence.xml file to an appropriate location. In your case, it should be a sibling with your sources. Basically, a sibling of your root packages, e.g. package and echo

The following is from the JPA spec:

A persistence.xml file defines a persistence unit. The persistence.xml file is 
located in the META-INF directory of the root of the persistence unit. 

The root of the persistence unit is the key here.

If you are a non-Java EE app

The jar file or directory whose META-INF directory contains the persistence.xml 
file is termed the root of the persistence unit.

If you are in a Java EE app, the following are valid

In Java EE environments, the root of a persistence unit must be one of the following:
• an EJB-JAR file
• the WEB-INF/classes directory of a WAR file[80]
• a jar file in the WEB-INF/lib directory of a WAR file
• a jar file in the EAR library directory
• an application client jar file
0
votes

Oddly enough I did not even have to add that.

The persistence provider was automatically done.

What I did to fix that was move the line above the persistence unit tag then recompile and redeploy, then move it back and recompile and redeploy and it worked.

No idea why it worked but it did