java - 缺少 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,如果重要的话
解决方案
显然问题是我制作的 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>
推荐阅读
- python - 使用 python exe 文件的添加/删除程序
- android - NoSuchMethodError: 类 Landroid/util/SparseArray 中没有包含 (I)Z 的虚拟方法;
- reactjs - 无法在 React Native 中导入本地照片。出现错误“这些文件都不存在:”
- flutter - 在另一个页面的异步匿名函数中使用初始化的字符串
- if-statement - 需要按等级过滤回复,去掉“错误”和不适用的回复
- prometheus - 平滑非计数器图
- python - Selenium 无法按索引遍历 div
- javascript - 是否可以通过补偿漂移使多个设备上的 setInterval 保持“同步”,还是我需要服务器同步?
- ios - Xamarin.IOS 插件,在 DLL 中查看捆绑资源
- c# - 将 PieChart 与 LiveCharts 和 WPF .NET Core 一起使用