EntityManager没有名为Postgres的EntityManager的持久提供商。 Maven项目
我刚开始使用JPA,由于某种原因,我遇到了这个错误:
2022年5月18日上午10:47:45 javax.persistence.spi.persistenceProviderResolverholder $ defaultPersistenceProviderResolver 日志警告:javax.persistence.spi ::找不到有效的提供者。 线程“ main” javax.persistence.persistenceException中的异常:否 EntityManager的持久性提供商名为Postgres at javax.persistence.persistence.createentitymanagerfactory(persistence.java:86) 在 javax.persistence.persistence.createentitymanagerfactory(persistence.java:55) 在main.stesjpa(main.java:17)上的main.main(main.java:64)
持久单位名称与代码中的单位匹配。这是证明:
public static void testJPA() {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("postgres");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Continent continent = new modelbase.Continent("1","Europe");
em.persist(continent);
Continent c = (Continent)em.createQuery(
"select e from ContinentEntity e where e.name='Europe'")
.getSingleResult();
c.setName("Africa");
em.getTransaction().commit();
em.close();
emf.close();
}
这是persistance.xml:
<?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="postgres"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:postgresql://localhost:5432/postgres"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password"
value="4563"/>
</properties>
</persistence-unit>
</persistence>
现在试图过去2个小时以来要克服此错误,似乎无法弄清楚原因。 这也是pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>LAB9</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.4</version>
</dependency>
<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.2.1</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
</project>
I just got started with JPA and for some reason I get this error:
May 18, 2022 10:47:45 AM
javax.persistence.spi.PersistenceProviderResolverHolder$DefaultPersistenceProviderResolver
log WARNING: javax.persistence.spi::No valid providers found.
Exception in thread "main" javax.persistence.PersistenceException: No
Persistence provider for EntityManager named postgres at
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:86)
at
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at Main.testJPA(Main.java:17) at Main.main(Main.java:64)
The persistance unit name matches the one in the code. Here's proof:
public static void testJPA() {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("postgres");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Continent continent = new modelbase.Continent("1","Europe");
em.persist(continent);
Continent c = (Continent)em.createQuery(
"select e from ContinentEntity e where e.name='Europe'")
.getSingleResult();
c.setName("Africa");
em.getTransaction().commit();
em.close();
emf.close();
}
This is the persistance.xml:
<?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="postgres"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:postgresql://localhost:5432/postgres"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password"
value="4563"/>
</properties>
</persistence-unit>
</persistence>
Been trying to get past this error for 2 hours now and can't seem to figure out why.
Also this is the pom.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>LAB9</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.4</version>
</dependency>
<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.2.1</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
</project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 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