微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Persistence Exception No Persistence provider for EntityManager named

如何解决Persistence Exception No Persistence provider for EntityManager named

我正在尝试在 Eclipse 中测试我的 hibernate-maven 应用程序,当我运行获取实体类名称方法时,我收到此异常:

jun. 01,2021 9:52:51 P. M.       
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 OtraReunionMas
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.linkedin.learning.otrareunionmas.utiles.EntityManagerUtil.getEntityManager(EntityManagerUtil.java:9)
at com.linkedin.learning.otrareunionmas.utiles.EntityManagerUtil.main(EntityManagerUtil.java:15)

这是persistence.xml:

<?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="OtraReunionMas" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
   <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:MysqL://localhost:3306
      /otrareunionmas"/>
      <property name="javax.persistence.jdbc.driver" value="com.MysqL.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="xxxx"/>
      <property name="javax.persistence.jdbc.password" value="xxxx"/>
      <property name="javax.persistence.schema-generation.database.action" value="update"/>
      <property name="hibernate.dialect" value = "org.hibernate.dialect.MysqL8Dialect"/>
   </properties>
 </persistence-unit>
 </persistence>

这是带有方法的类:

 package com.linkedin.learning.otrareunionmas.utiles;

 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;

 public class EntityManagerUtil {

 public static EntityManager getEntityManager() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("OtraReunionMas");
    EntityManager manager = factory.createEntityManager();
    return manager;
 }

public static void main(String[] args) {
    EntityManager manager = EntityManagerUtil.getEntityManager();
    System.out.println("EntityManager class ==> " + manager.getClass().getCanonicalName());
}
}

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>com.linkedin.learning</groupId>
<artifactId>otrareunionmas</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>otrareunionmas</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <maven.compiler.source>1.7</maven.compiler.source>
   <maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/MysqL/mysql-connector-java -->
   <dependency>
     <groupId>MysqL</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>8.0.25</version>
   </dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.4.16.Final</version>
  <type>pom</type>
</dependency>

</dependencies>
      <artifactId>maven-clean-plugin</artifactId>
      <version>3.1.0</version>
    </plugin>
    <!-- default lifecycle,jar packaging: see https://maven.apache.org/ref/current/maven-
     core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <version>3.0.2</version>
    </plugin>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.0</version>
    </plugin>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.22.1</version>
    </plugin>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
    </plugin>
    <plugin>
      <artifactId>maven-install-plugin</artifactId>
      <version>2.5.2</version>
    </plugin>
    <plugin>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>2.8.2</version>
    </plugin>
    <!-- site lifecycle,see https://maven.apache.org/ref/current/maven-
    core/lifecycles.html#site_Lifecycle -->
    <plugin>
      <artifactId>maven-site-plugin</artifactId>
      <version>3.7.1</version>
    </plugin>
    <plugin>
      <artifactId>maven-project-info-reports-plugin</artifactId>
      <version>3.0.0</version>
    </plugin>
  </plugins>
</pluginManagement>
</build>
</project>

我尝试更改持久性的版本号、更改 xmlns、提供程序的名称,但仍然是例外。

解决方法

<type>pom</type> 处为 hibernate-core 去掉 pom.xml

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。