【问题标题】:Hibernate - No Persistence provider for EntityManagerHibernate - EntityManager 没有持久性提供程序
【发布时间】:2026-02-06 21:05:02
【问题描述】:

我知道已经有很多关于这个主题的问题,但到目前为止我已经尝试了所有方法并阅读了我能找到的所有内容,但它仍然没有解决我的问题。

我在 Eclipse 中使用 Hibernate 5.2.1 开发了一个 Java 应用程序。 Final 和 SQLite JDBC (Xerial 3.8.11.2) 和 Maven。每当我尝试从 Eclipse 运行项目时,都会遇到这个讨厌的异常:

    Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$156(LauncherImpl.java:352)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named database
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at pl.nombritech.Produktor.Main.<clinit>(Main.java:18)
    ... 11 more
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    ... 5 more

当然,我检查了我的 META-INF 目录是否在正确的位置 (src/main/resources/META-INF/persistence.xml) 以及它是否包含在类路径中。持久化单元名称也与 Main 类中给出的名称相对应:

public static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory(
    "database");

我不知道为什么会这样。每当我尝试从 .jar 启动它时,我都会遇到另一个异常:

    Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: javax/persistence/Persistence
        at pl.nombritech.Produktor.Main.<clinit>(Main.java:18)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$156(LauncherImpl.java:352)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 12 more
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NullPointerException
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        ... 5 more

最后附上我的 POM.xml 和 persistence.xml:

POM.xml:

    <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>pl.nombritech</groupId>
    <artifactId>Produktor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Produktor</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- LIBRARIES IN LIB DIRECTORY -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!-- <classpathPrefix>lib</classpathPrefix> -->
                            <mainClass>pl.nombritech.Produktor.Main</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>lib/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.8.11.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>


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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

persistence.xml:

    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="database" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
            <property name="javax.persistence.jdbc.user" value="" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:bazadb.db" />
            <property name="hibernate.dialect" value="pl.nombritech.Produktor.util.SQLiteDialect" />
            <!-- create-drop tworzy i kasuje, create tworzy tylko -->
            <property name="hibernate.hbm2ddl.auto" value="create"></property>
        </properties>
    </persistence-unit>
</persistence>

感谢所有帮助。请不要写它是重复的,因为我在这些主题中搜索了解决方案,但没有一个对我有帮助。非常感谢您的帮助。

PS。我尝试使用 Hibernate 的先前主要版本(4.3.11.Final - 我想我只使用了另一个提供程序:org.hibernate.ejb.HibernatePersistence,它现在已被弃用并且在 Hibernate-Core 5 中不存在)并且它作为一个魅力,所以我真的不知道版本 5 中发生了什么,它的行为如此不同。

编辑:

我在 Eclipse 中的项目结构是:

Project structure in Eclipse

EDIT3:

上传我的项目(我自己的托管):

Download my project (Eclipse Project)

【问题讨论】:

  • 文件应该命名为persistence.xml,而不是persistance.xml
  • 对不起,我发错了,当然是“persistence.xml”,它不起作用:(
  • 您的 Eclipse 项目似乎一团糟:ClassNotFoundException: javax.persistence.Persistence。尝试重新导入 Maven 项目。为什么你的 pom 中同时有 hibernate-jpa-2.1-api 和 javax.persistence?
  • 我想我应该补充一点,以防万一 javax.persistence.Persistence 不包括在内,正如我所说,我已经尽一切努力让它运行 :) 我会稍后尝试重新导入,看看会发生什么
  • @JBNizet 什么都没变当我从 Eclipse 运行它时没有持久性提供程序?这只是没有意义......

标签: java eclipse hibernate maven jpa


【解决方案1】:

为什么要混合使用 eclipseLink 和 hibernate? 使用

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.2.1.Final</version>
</dependency>

并删除 eclipseLink 依赖项

你的复制依赖目标应该有

<outputDirectory>${project.build.directory}/lib</outputDirectory>

最后,在构建 jar 时,在你的 maven-jar-plugin 中使用这个 sn-p。罐子应该在你的主罐子外面,而不是里面

                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib</classpathPrefix>
                            <mainClass>pl.nombritech.Produktor.Main</mainClass>
                        </manifest>
                    </archive>

【讨论】:

  • 我只留下了那个(hibernate-entitymanager),我仍然收到No Persistence provider for EntityManager named database...我真的不知道该怎么办:(
  • @Bloodlex 你确定你的 persistence.xml 被复制到目标文件夹吗?
  • “target”文件夹中persistence.xml的路径是:target/classes/META-INF/persistence.xml
  • 现在这很奇怪......你能把你的项目上传到某个地方吗?
  • 我在主机上上传了我的项目,您可以在主帖末尾找到链接 :) 我希望它有所帮助,因为我走到了死胡同 :(