首页 > 解决方案 > 缺少 Hibernate ServiceRegistry 类?

问题描述

我开始使用休眠,我正在关注本教程

https://www.javaguides.net/2018/11/hibernate-hello-world-tutorial.html

但是当我运行我的罐子时,我得到了

Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry

这很不言自明,但我不知道如何得到它

我试过添加

import org.hibernate.service.ServiceRegistry;

但没有运气

本教程的最小结构是:

pom文件:

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.13</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.7.Final</version>
    </dependency>
</dependencies>

应用程序.java

package net.javaguides.hibernate;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class App {
    public static void main(String[] args) {
        Student student = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");
        Transaction transaction = null;

        try (Session session = HibernateUtil.getSessionFactory().openSession()) {
            transaction = session.beginTransaction();
            session.save(student);
            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }
}

HibernateUtil.java

package net.javaguides.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateUtil {
    private static StandardServiceRegistry registry;
    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                registry = new StandardServiceRegistryBuilder().configure().build();

                MetadataSources sources = new MetadataSources(registry);

                Metadata metadata = sources.getMetadataBuilder().build();

                sessionFactory = metadata.getSessionFactoryBuilder().build();

            } catch (Exception e) {
                e.printStackTrace();
                if (registry != null) {
                    StandardServiceRegistryBuilder.destroy(registry);
                }
            }
        }
        return sessionFactory;
    }

    public static void shutdown() {
        if (registry != null) {
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }
}

我正在使用 Manjaro Linux,如果重要的话

标签: javahibernate

解决方案


显然问题是我制作的 jar 中缺少 Hibernate 本身(它是目标文件夹)

解决方案是将依赖项打包在 jar 本身或目标文件夹中(并让类路径引用它们?)

我选择了第一个,但这会减慢重新编译的速度:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.fistmedia.javapp.App</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

推荐阅读