javax.persistence.PersistenceException: No Persistence provider for EntityManager named
Asked Answered
E

2

8

I'm trying to set up a simple jpa 2.0 project by following the information provided by my teacher's documentation . I've been on this for hours now, but no matter what I do I always get this exception when I try to create a EntityManagerFactory: I've found quite a few similar questions regarding this exception, but no solutions that I am able to get to work. What am I doing wrong here?

I created this project from Eclipse (no command prompt)

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named course
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
    at message.SaveMessage.main(SaveMessage.java:8)

directory structure

enter image description here

my persistence.xml

<persistence 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"
    version="2.0">
    <persistence-unit name="course" transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>


        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/StudentDB" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="pasapas2005" />
        </properties>
    </persistence-unit>
</persistence>

My class

package message;

import java.io.Serializable;

import javax.persistence.*;

@Entity
public class Message implements Serializable {

    private long id;
    private String text;

    public Message() {

    }

    public Message(long id, String text) {
        this.setId(id);
        this.setText(text);

    }

    @Id
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

My my tester (main) class

package message;

import javax.persistence.*;

public class SaveMessage {

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

        Message message = new Message(1, "Hello world");
        em.persist(message);
        tx.commit();
        em.close();
        System.out.println("message saved");

    }

}
Ennui answered 20/5, 2013 at 18:18 Comment(5)
Check that the class org.hibernate.ejb.Hibernat ePersistence is on your classpath.Mehalek
Thank you for replying. My question may sound stupid but how do i check the my classpath ?Ennui
In eclipse, with your project open, press Ctrl+Shift+T and start to type the class name. If it doesn't appear in the list, you know it is not on classpath.Mehalek
it doesnt appear. I'll do the necessary thank youEnnui
Hey, Could you please share your pom.xml file?Lauretta
M
7

I think the class org.hibernate.ejb.Hibernat ePersistence is missing from your classpath. Add it to your pom.xml:

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

Replace 3.6.10.Final with the appropriate version of Hibernate.

Mehalek answered 20/5, 2013 at 18:38 Comment(3)
Not completely fixed but alteast the error message is gone leaving place to a new error message saying Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory. But now i think i know how to get it to workEnnui
Looks like you are missing slf4j too.Mehalek
Note: I had this exception after moving a class to a new package missing to reflect the change the persistence.xml.Isabea
M
0

I had the same issue. After a lot of search i found that the solution was just to install from eclipse marketplace the HiberObjects.

Matsuyama answered 4/5, 2017 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.