Eclipselink“没有持久性提供者的EntityManager命名为...”例外
我正在尝试创建一个简单的Java应用程序,使用JPA与MySQL数据库进行通信,这使我能够将新行插入数据库中的表中,检索其中的数据等等。但是,当我尝试运行它时,我遇到了这个错误:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named default
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.lab9.Main.main(Main.java:9)
我知道这里已经问了这个问题几次,但是没有一个旧线程对我有任何帮助。通常,人们似乎会得到此例外,因为他们在错误的目录中具有persistence.xml
文件,或者因为他们忘记在上述文件中指定< provider>
我都不是这样。这就是我的项目结构在Intellij中的样子:
如您所见,我在persistence.xml
中,src/main/resources/resources/meta-inf
,应该在哪里。在此文件中,我有一个:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<class>com.lab9.Continent</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cities"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
</persistence>
最后,在我的pom.xml
文件中,我添加了这些依赖项:
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>
我尝试了几乎所有的操作:移动文件,添加和删除依赖项,重新编写整个程序从头开始,但似乎没有任何作用。
I'm trying to create a simple Java app, using JPA to communicate with a MySQL database, that allows me to insert new rows into the tables found in the database, retrieve the data inside them, etc. But when I try running it, I get this error:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named default
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.lab9.Main.main(Main.java:9)
I know this question has already been asked a few times on here, but none of the older threads have been of any help to me. Usually, people seem to get this exception because they have their persistence.xml
file in the wrong directory, or because they forgot to specify a <provider>
in said file, neither of which is the case for me. This is what my project structure looks like in IntelliJ:
As you can see, I have persistence.xml
in src/main/resources/META-INF
, where it's supposed to be. Inside this file, I have this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<class>com.lab9.Continent</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cities"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
</persistence>
And finally, in my pom.xml
file, I added these dependencies:
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>
I've tried pretty much everything: moving files around, adding and deleting dependencies, re-writing the whole program from scratch, yet nothing seems to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用基于Jakarta XSD(3.0)的最新版本的持久性XML,但使用Javax.persistence连接到数据库。
您必须使用Juste:
org.hibernate.jpa.hibernatepersistenceProvider
,并在your pom.xml中包含folloin文物
You use a latest version of persistence xml based on jakarta xsd (3.0) but you use javax.persistence to connect to your database.
You juste have to use :
org.hibernate.jpa.HibernatePersistenceProvider
and include the folloin artifacte in you'r pom.xml