我已经使用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)
我已经检查了堆栈中描述的所有类似问题,但是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>
通过调试,错误发生在以下语句中:
private static void deleteFromDB() {
EntityManager em;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MovieDatabasePU"); <-- This line
em = emf.createEntityManager();
em.getTransaction().begin();
还有什么可能是错的,因为我尝试了几乎所有解决方案,但仍然没有数据库连接。
最佳答案
您的persistence.xml
应该在/src/main/resources/META-INF/
文件夹中,而不是在src/META-INF
中。
关于java - 持久性单元错误:javax.persistence.PersistenceException:没有名为EntityManager的持久性提供程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54747051/