我在IntelliJ中收到EntityManager的异常无持久性提供程序

时间:2016-11-20 09:44:39

标签: java eclipse hibernate intellij-idea persistence

有人知道问题出在哪里 - image

  

在类路径中找不到任何META-INF / persistence.xml文件

我使用IntelliJ和h2数据库连接Hibernate

有我的课程:

持久性:

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress DeprecatedClassUsageInspection -->
<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="StudentPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>model.StudentInfo</class>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.url" value="jdbc:h2:~/JPA_App" />
            <property name="hibernate.connection.user" value="sa" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.temp.use_jdbc_metadata_defaults"
                      value="false" />
        </properties>

    </persistence-unit>
</persistence>

实体类:

package model;

import javax.persistence.*;

@Entity
@Table(name = "Student")
public class StudentInfo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "name", length = 30, nullable = false)
    private String name;
    @Column(name = "lastname", length = 30, nullable = false)
    private String lastname;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
}

和主要课程

package model;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class AddStudent {

    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("StudentPU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();

        StudentInfo student = new StudentInfo();
        student.setId(0);
        student.setName("John");
        student.setLastname("Doe");

        em.persist(student);
        em.getTransaction().commit();

        em.close();
        emf.close();

    }
}

在eclipse中这个简单的源码有效,但是在IntelliJ中我收到了这个例外。 我很高兴有任何想法来解决这个错误。

致以最诚挚的问候,

d。 Balamjiev

1 个答案:

答案 0 :(得分:0)

只需将您的META-INF文件夹移动到IDE的某个构建路径的根目录即可 例如,将META-INF文件夹移动到web文件夹,它应该在运行时可用。

通过使用Maven或Gradle等构建工具,您不会再遇到这类问题,因为布局是标准布局,因此无论您使用何种IDE,它都是相同的。
例如,在您的情况下,它将同时适用于Eclipse和IntelliJ:src/main/resources/META-INF