我对休眠框架还很陌生,并且正在使用Udemy课程创建一个简单的应用程序。我一直在下面的堆栈上得到一个“ java.lang.NoClassDefFoundError”。似乎当我创建org.hibernate.cfg.Configuration对象时,会引发异常。关于如何解决以下问题的任何指导都将受到赞赏,此休眠版本是否有故障,是否需要回溯到以前的版本?
休眠核心版本:5.3.0.Final
Hibernate注释:3.5.6.Final
My-SQL Server版本:8.0.12
DEBUG - Logging Provider: org.jboss.logging.Log4jLoggerProvider
DEBUG - Adding Integrator [org.hibernate.cfg.beanvalidation.BeanValidationIntegrator].
DEBUG - Adding Integrator [org.hibernate.secure.spi.JaccIntegrator].
DEBUG - Adding Integrator [org.hibernate.cache.internal.CollectionCacheInvalidator].
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43)
at org.hibernate.boot.MetadataSources.<init>(MetadataSources.java:86)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:123)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:118)
at com.dataPack.data.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
at com.dataPack.data.HibernateUtil.<clinit>(HibernateUtil.java:10)
at com.dataPack.data.Application.main(Application.java:9)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 7 more
这是我创建的用于构建sessionFactory的HibernateUtil类。
package com.dataPack.data;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
Configuration configuration = null;
try {
configuration = new Configuration();
return configuration
.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.build());
} catch(Exception e) {
e.printStackTrace();
throw new RuntimeException("Issue Building Session Factory!");
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
这是我们应该使用的hibernate.properties文件。
hibernate.connection.username=user
hibernate.connection.password=password
hibernate.connection.url=jdbc:mysql://localhost:3306/ifinances
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
答案 0 :(得分:0)
由于该项目有效,因此其他人可能使用了不同的Java版本,因为jaxb Apis已从Java SE中删除。有多种解决方法(如How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9中所述),但最可靠的方法是将jaxb API依赖项(groupId javax.xml.bind,artifactId jaxb-api-https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api/2.3.0)添加到pom .xml或gradle构建文件。
然后重建,如果仍然遇到ClassNotFound错误,请参见https://stackoverflow.com/a/43574427/9705485