gpt4 book ai didi

javax.persistence.PersistenceException : No Persistence provider for EntityManager named

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:28:06 30 4
gpt4 key购买 nike

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

我的类(class)

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;
}

}

我的测试员(主要)类(class)

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。

关于javax.persistence.PersistenceException : No Persistence provider for EntityManager named,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16655750/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com