javax.persistence.PersistenceException:没有名为EntityManager的持久性提供程序

时间:2013-05-20 18:18:10

标签: java hibernate maven persistence.xml

我正在尝试按照我老师的文档提供的信息设置一个简单的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)

目录结构

enter image description here

我的 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");

    }

}

2 个答案:

答案 0 :(得分:5)

我认为类路径中缺少类org.hibernate.ejb.Hibernat ePersistence。将它添加到您的pom.xml:

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

将3.6.10.Final替换为适当版本的Hibernate。

答案 1 :(得分:0)

我有同样的问题。经过大量搜索,我发现解决方案只是从eclipse市场安装 HiberObjects。