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

前端之家收集整理的这篇文章主要介绍了javax.persistence.PersistenceException:没有名为EntityManager的持久性提供程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试按照我老师的文档提供的信息建立一个简单的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");

    }

}

解决方法

我认为类路径中缺少类org.hibernate.ejb.Hibernat ePersistence.将它添加到您的pom.xml:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.10.Final</version>
</dependency>

用适当版本的Hibernate替换3.6.10.Final.

猜你在找的Java相关文章