如何修复ClassNotFoundException:休眠项目intellij中的javax.persistence.Persistence

时间:2019-05-16 21:55:24

标签: hibernate maven intellij-idea

我不知道我在做什么错:C请帮助我c:

public static void main (String[] arg){
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("PruebaPU");
        EntityManager em = emf.createEntityManager();
    }

我读到它可能是依赖项,但是我添加了休眠和javax依赖项

JDK版本:1.8.0

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.thorntail</groupId>
        <artifactId>bom-all</artifactId>
        <version>${version.thorntail}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>



        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.2.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.16</version>
      </dependency>



    </dependencies>
  </dependencyManagement>



javax依赖项的末尾

</build>

  <dependencies>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </dependency>

  </dependencies>
</project>

这是我运行项目时的异常。

Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Persistence
    at utn.dlc.Prueba.rest.Program.main(Program.java:8)
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

您需要添加Maven依赖项

<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>

提供缺少的课程