8

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

    }

}
4

2 回答 2

7

我认为您的类路径中缺少 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。

于 2013-05-20T18:38:02.537 回答
0

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

于 2017-05-04T14:20:17.343 回答