持久性单元错误:javax.persistence.PersistenceException:没有名为EntityManager的持久性提供程序

时间:2019-02-18 12:12:52

标签: java eclipse jpa derby

我已经使用Netbeans在Java中的OSX中创建了一个项目,并与在github上Windows上工作的同事一起工作。

一旦克隆了项目,它们就会出现以下错误,表明该项目无法使用Eclipse连接到derby数据库。

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named MovieDatabasePU
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at moviedatabase.MovieDatabase.deleteFromDB(MovieDatabase.java:126)
    at moviedatabase.MovieDatabase.main(MovieDatabase.java:48)
C:\Users\Mixalis\Documents\NetBeansProjects\Homework3\Ergasia3\nbproject\build-impl.xml:1076: The following error occurred while executing this line:
C:\Users\Mixalis\Documents\NetBeansProjects\Homework3\Ergasia3\nbproject\build-impl.xml:830: Java returned: 1
BUILD FAILED (total time: 2 seconds)

我已经检查了Stack上描述的所有类似问题,但是persistence.xml是正确的,它位于src / META-INF文件夹中的META-INF文件夹中。

下面是持久性文件结构。

<?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="MovieDatabasePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>moviedatabase.entities.FavoriteList</class>
    <class>moviedatabase.entities.Genre</class>
    <class>moviedatabase.entities.Movie</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/person"/>
      <property name="javax.persistence.jdbc.user" value="pli24"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.password" value="pli24"/>
    </properties>
  </persistence-unit>
</persistence>

enter image description here

从调试中,错误发生在以下语句上:

 private static void deleteFromDB() {
        EntityManager em;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("MovieDatabasePU"); <-- This line

        em = emf.createEntityManager();
        em.getTransaction().begin();

还有什么问题呢,因为我尝试了几乎所有解决方案,但仍然没有数据库连接。

2 个答案:

答案 0 :(得分:1)

您的persistence.xml应该在/src/main/resources/META-INF/文件夹中,而不是src/META-INF文件夹中。

答案 1 :(得分:0)

好吧,我只需要进行清理和构建即可使其正常运行!!! 所有文件都还可以,仅清理还不够。