gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:44:20 25 4
gpt4 key购买 nike

我是 JPA 和 Hibernate 的新手。我正在观看教程,并且我做了视频中提到的所有操作,但我认为有些错误。

我正在使用 eclipse java ee。还有jpa、hibernate、MySQL

坚持:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="aramis" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.webproject.web.entities.User</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/webproject" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="mysql123" />
</properties>
</persistence-unit>
</persistence>

用户:

@Entity(name="user")
@Table(name="user")
public class User implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", updatable = false, nullable = false)
private int id;

private String name;

@Column(nullable=false, unique=true)
...

服务: 公共(public)类 UserService {

    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("aramis");
private EntityManager em = emf.createEntityManager();
private EntityTransaction tx = em.getTransaction();

private Sha512 sha512 = new Sha512();


public int insert(User user) {
Date date = new Date();
user.setCreated_at(date);
user.setRole_type(0);

String password = user.getPassword();
password = sha512.encrypt(password);
user.setPassword(password);

tx.begin();
em.persist(user);
tx.commit();
em.close();

return user.getId();
}

它给出了一些错误:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named aramis
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at com.webproject.web.services.UserService.<init>(UserService.java:19)
at com.webproject.web.servlet.Register.doPost(Register.java:54)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

还有我的路径: path

最佳答案

默认情况下,当您调用 Persistence.createEntityManagerFactory("aramis") 时,它会尝试在名为 META-INF 的文件夹中查找名为 persistence.xml 的文件

为了使其正常工作,您的类路径中应该有一个META-INF/persistence.xml

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

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