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

时间:2018-12-25 10:27:17

标签: java hibernate jpa persistence

我是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)
                  ...

服务:         公共类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

1 个答案:

答案 0 :(得分:1)

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

要使其正常运行,您的类路径中应该有一个META-INF/persistence.xml