0

I'm trying to develop a Java EE web application using JPA to manage the DB.

When I try to retrieve the rows from my database I've an error :

"No Persistence provider for EntityManager named test"

The code of my function using JPA is :

EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");      
     EntityManager em =emf.createEntityManager();        
     EntityTransaction tx = em.getTransaction();
     try {
         tx.begin();
          @SuppressWarnings("unchecked")
          List<InputHStock> iph = em.createQuery("from Student").getResultList();
          for (Iterator<InputHStock> iterator = iph.iterator(); iterator.hasNext();) {
            InputHStock student = (InputHStock) iterator.next();
            System.out.println(student.getLocationCode());
          }
          tx.commit();
        } catch (Exception e) {
          tx.rollback();
        }   
}

My persistence.xml file :

<?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="test">
    <class>application.InputHStock</class>
     <properties>
      <property name="javax.persistence.jdbc.driver"     value="oracle.jdbc.driver.OracleDriver" />
      <property name="javax.persistence.jdbc.url"    value="jdbc:oracle:thin:@localhost:1521:xe" />
      <property name="javax.persistence.jdbc.user" value="testSQL" />
      <property name="javax.persistence.jdbc.password" value="testpwd1" />
      <property name="eclipselink.ddl-generation" value="create-tables" />
      <property name="eclipselink.ddl-generation.output-mode" value="database" />
      </properties>
    </persistence-unit>
</persistence>

Here is my arborescence in eclipse :

enter image description here

I've also tried using the Eclipse tool to include JPA but without success.

Note that the connection to the database is working as I can retrieve my data when using directly JDBC.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Soji
  • 177
  • 1
  • 5
  • 17

3 Answers3

0

Your persistence.xml is lacking a persistence provider. As you use EclipseLink, add

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

inside of the persistence-unit tag.

Smutje
  • 17,733
  • 4
  • 24
  • 41
0

I think "hibernate-entitymanager" dependency missing in your pom.xml

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.3.6.Final</version>
</dependency>

Probably you are using hibernate-core in pom. Use hibernate-entitymanager instead of hibernate-core for JPA.

Also requires provider tag in persistence.xml.

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
Narayan Yerrabachu
  • 1,714
  • 1
  • 19
  • 31
0

I was dealing with this error in a somewhat misleading scenario. Hibernate dependency was correctly added, persistence.xml was fine.

Turns out I hadn't included the database driver dependency on Maven.

I thought it wouldn't be relevant to my test, since I was not going to hit a DB. Just wanted to test the Query API.

GFonte
  • 451
  • 6
  • 10