gpt4 book ai didi

java - Hibernate ServiceRegistry 类丢失?

转载 作者:行者123 更新时间:2023-12-01 17:43:59 25 4
gpt4 key购买 nike

我正在开始使用 hibernate,并且正在遵循本教程

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

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

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

最佳答案

显然问题是我生成的 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>

关于java - Hibernate ServiceRegistry 类丢失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60896542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com