Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javax.persistence.PersistenceException:没有名为_Java_Hibernate_Maven_Persistence.xml - Fatal编程技术网

javax.persistence.PersistenceException:没有名为

javax.persistence.PersistenceException:没有名为,java,hibernate,maven,persistence.xml,Java,Hibernate,Maven,Persistence.xml,我正试图按照老师的文档提供的信息建立一个简单的JPA2.0项目。我已经做了几个小时了,但无论我做什么,当我尝试创建EntityManagerFactory时,总是会遇到这样的异常: 关于这个例外,我发现了很多类似的问题,但没有解决方案。我做错了什么 我从Eclipse创建了这个项目(没有命令提示符) 目录结构 mypersistence.xml <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xs

我正试图按照老师的文档提供的信息建立一个简单的JPA2.0项目。我已经做了几个小时了,但无论我做什么,当我尝试创建EntityManagerFactory时,总是会遇到这样的异常: 关于这个例外,我发现了很多类似的问题,但没有解决方案。我做错了什么

我从Eclipse创建了这个项目(没有命令提示符)

目录结构

mypersistence.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>
我的测试仪(主)类


我认为类org.hibernate.ejb.Hibernat ePersistence在类路径中丢失。将其添加到pom.xml中:

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

org.hibernate
休眠实体管理器
3.6.10.最终版本

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

我也遇到了同样的问题。经过大量搜索,我发现解决方案只是从eclipse marketplace安装HiberObject。

检查类org.hibernate.ejb.Hibernat ePersistence是否在您的类路径上。感谢您的回复。我的问题可能听起来很愚蠢,但是我如何检查我的类路径呢?在eclipse中,在项目打开的情况下,按Ctrl+Shift+T并开始键入类名。如果它没有出现在列表中,您就知道它不在类路径上。它不会出现。我会做必要的事情谢谢你嘿,你能分享你的pom.xml文件吗?虽然没有完全修复,但是错误消息已经消失了,在线程“main”java.lang.NoClassDefFoundError:org/slf4j/LoggerFactory中出现了一条新的错误消息,上面写着Exception。但现在我想我知道了如何将它放到工作本中,就像你也缺少slf4j一样。注意:我在将一个类移动到一个新的包中后出现了这个异常,该包丢失了,以反映persistence.xml的更改。
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");

    }

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