我正在尝试按照我老师的文档提供的信息来设置一个简单的 jpa 2.0 项目。我已经做了几个小时了,但是无论我做什么,当我尝试创建一个 EntityManagerFactory 时,我总是会遇到这个异常:我发现了很多关于这个异常的类似问题,但没有我能够的解决方案开始工作。我在这里做错了什么?
我从 Eclipse 创建了这个项目(没有命令提示符)
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)
目录结构
我的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>
我的课
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;
}
}
我的测试人员(主要)课程
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");
}
}