Tomasz Waszczyk
Tomasz Waszczyk

Reputation: 3129

No persistence provider for EntityManager, JPA configuration

I try to configure JPA Hibernate with the following persistence.xml :

<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="manager1" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <mapping-file>orm.xml</mapping-file>

    <properties>
        <property name="javax.persistence.jdbc.driver" value="x" />
        <property name="javax.persistence.jdbc.url"
            value="x;create=true" />
        <property name="javax.persistence.jdbc.user" value="x" />
        <property name="javax.persistence.jdbc.password" value="x" />
        <property name="dialect" value="org.hibernate.dialect.DB2Dialect" />
        <!-- EclipseLink should create the database schema automatically -->
        <!-- <property name="eclipselink.ddl-generation" value="create-tables" 
            /> <property name="eclipselink.ddl-generation.output-mode" value="database" 
            /> -->
    </properties>

</persistence-unit>

in the place "x" there are proper values, but I got:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named manager1
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at de.volkswagen.webpromet.Main.main(Main.java:10)

Thanks for help ! ! !

Upvotes: 2

Views: 9816

Answers (4)

Ujwal Daware
Ujwal Daware

Reputation: 391

Add your persistence.xml in the META-INF folder and create the persistence like this way:

    <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="abc">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!-- map your classes. -->
    <properties>
    <property name="javax.persistence.jdbc.driver" value="x" />
    <property name="javax.persistence.jdbc.url"value="x/>
    <property name="javax.persistence.jdbc.user" value="x" />
    <property name="javax.persistence.jdbc.password" value="x" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    <property name="hibernate.show_sql" value="true" />
    </properties>
        </persistence-unit>
    </persistence>

Create a Java class HibernateUtil.java and provide the persistence unit name to entityManagerFactory = Persistence.createEntityManagerFactory("abc");

public class HibernateUtil {

    private static final EntityManagerFactory entityManagerFactory;
    static {
                try {
                     entityManagerFactory = Persistence.createEntityManagerFactory("abc");
                     System.out.println("Entity Menager Test.............."+ entityManagerFactory);
                } catch (Throwable ex) {

                    System.err.println("Initial SessionFactory creation failed." + ex);
                    throw new ExceptionInInitializerError(ex);

                  }
    }

public static EntityManagerFactory getEntityManagerFactory() {
         return entityManagerFactory;
    }

}

Try it, it will work.

Upvotes: 2

cmd
cmd

Reputation: 11841

You will need to place the persistence.xml file in an appropriate location. In your case, add the META-INF/persistence.xml file to the root of a source folder.

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.

Since you are working with 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

However, if you were creating a non-Java EE app, the following apply:

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

Upvotes: 0

Tomasz Waszczyk
Tomasz Waszczyk

Reputation: 3129

It is the place clearly showed where should be placed persistence.xml

TIP: Documentation is the best TUTORIAL !

src
|-- main
|   |-- java
|   `-- resources
|       |-- jpa
|       |   |-- Clerk-orm.xml
|       |   |-- Customer-orm.xml
|       |   |-- Person-orm.xml
|       |   `-- Sale-orm.xml
|       `-- META-INF
|           `-- persistence.xml
`-- test
    `-- resources
        `-- hibernate.properties

More here: http://webdev.apl.jhu.edu/~jcs/ejava-javaee/coursedocs/605-784-site/docs/content/html/hibernate-migration-orm.html#hibernate-migration-orm-mapping

Upvotes: 3

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

If you are placing the persistence.xml file within the WEB-INF directory it is never making its way onto the classpath. Add the META-INF/persistence.xml file to the root of a source folder.

Upvotes: 1

Related Questions