gpt4 book ai didi

java - hibernate 。配置。不启动 jar 文件

转载 作者:行者123 更新时间:2023-12-02 11:56:23 26 4
gpt4 key购买 nike

我通过 Maven 将项目打包到 jar 中。当我启动 jar 文件时,我发现有异常(exception):

Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:12)
at util.HibernateUtil.<clinit>(HibernateUtil.java:8)
at model.services.BusService.setBuses(BusService.java:30)
at view.Runner.main(Runner.java:20)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
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)
... 4 more

我的 hibernate.cfg.xml

<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>
<session-factory>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/carpark?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">1111</property>

<property name="hibernate.hbm2ddl.auto">update</property>

<mapping class="model.entities.Bus"/>
<mapping class="model.entities.Driver"/>
<mapping class="model.entities.Rote"/>
</session-factory>

我的pom.xml

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>view.Runner</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>hibernate.cfg.xml</directory>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.11.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>

我的HibernateUtil.java

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure().buildSessionFactory();
} catch (Exception ex) {
return (SessionFactory) new Exception(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
getSessionFactory().close();
}}

我的 BusService.java

package model.services;

import model.entities.Bus;
import model.repositories.BusRepository;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;

public class BusService {
private BusRepository busRepository;

public BusService() {
busRepository = BusRepository.getInstance();
}

public BusRepository getBusRepository() {
return busRepository;
}

public List<Bus> getBuses() {
return busRepository.getBuses();
}

public void setBuses() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
SQLQuery query = session.createSQLQuery("SELECT ID, NUMBER , Rote_ID FROM bus").addEntity(Bus.class);
List<Bus> buses = query.list();
buses.size();
busRepository.setBuses(buses);
} finally {
tx.commit();
session.close();
}
}

public List<Bus> findByNumber(Integer number) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
List<Bus> busesFinder = new ArrayList<Bus>();
try {
SQLQuery query = session.createSQLQuery("SELECT ID, NUMBER , Rote_ID FROM bus");
List<Bus> buses = query.list();
for (Bus bus : buses) {
if (bus.getNumber() == number)
busesFinder.add(bus);
}
return busesFinder;
} finally {
tx.commit();
session.close();
}
}

public void save(Bus bus) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
session.save(bus);
} finally {
tx.commit();
session.close();
}
}

}

BusService.java 中的第 30 行是(在方法 setBuses() 中)

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

我使用了另一个hibernate版本,但每次都有异常(exception)。现在我的 hibernate 版本是5.0.11.Final。

请帮我解决这个问题。我需要正确启动 jar 文件,没有异常(exception)。

最佳答案

JAR 文件通常不包含库依赖项(与 WAR 文件不同),因此您需要使用 maven-shade-plugin 等工具手动添加它们:

https://maven.apache.org/plugins/maven-shade-plugin/

关于java - hibernate 。配置。不启动 jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47575596/

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