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
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");
}
}
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.