2016-11-20 45 views
0

有人來知道在哪裏可以是問題 - image我收到異常沒有持久性提供的EntityManager中的IntelliJ

找不到任何META-INF/persistence.xml文件在classpath

我使用的IntelliJ和與Hibernate

H2數據庫連接

有我的課:

持久性:

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

和Main類

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

回答

0

只是在你的IDE的構建路徑之一的根移動您META-INF文件夾。
例如,將META-INF文件夾移動到web文件夾,它應該在運行時可用。

使用諸如Maven或Gradle等構建工具,您不會遇到這些問題,因爲佈局是標準的,因此無論您使用哪種編輯器都是如此。
例如,在您的示例中,它將同時在Eclipse和IntelliJ中:src/main/resources/META-INF

相關問題