Hibernate – No Persistence provider for EntityManager Hibernate

hibernatejpa

I am having a No Persistence provider for EntityManager exception, and can't figure out what is causing it. Here is my configuration file:

Persistence.xml (stored in src/META-INF)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/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">
    <persistence-unit name="HatifimJPA" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>entities.HatUser</class>
        <properties>
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
            <property name="hibernate.connection.username" value="Benny" />
            <property name="hibernate.connection.password" value="oracle" />
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:xe" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
        </properties>
    </persistence-unit>
</persistence>

Exception:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named HatifimJPA
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at testing.TestClass.main(TestClass.java:16)

The only difference is that I am trying to get the EntityManager into a public void main(...) { ... } block, however, I don't recall having a problem doing that in the past.

Can anyone help to point out where my problem might reside?

Best Answer

Try putting the persistence.xml to src/main/resources/META-INF/ and put this code to the main method:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("HatifimJPA");
EntityManager em = emf.createEntityManager();
Related Topic