在Java项目java.lang中使用Hibernate时出现异常:异常[重复]

yv5phkfx  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(205)
    • 此问题在此处已有答案**:

How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException(42个答案)
Hibernate Session Factory javax/xml/bind/JAXBException error(5个答案)
2天前关闭。
我尝试将Hibernate用于使用hibernate.cfg.xml的Java项目。我按照教程添加了所有必需的jar,但每当我运行应用程序时,我都会收到NoClassDefFoundError:

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:87)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:123)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:118)
    at org.studyeasy.hibernate.App.main(App.java:12)

User.java:

package org.studyeasy.hibernate.entity;

import javax.persistence.Column;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user")
public class User {

    @Id
    @Column(name = "id")
    int id;

    @Column(name = "username")
    String username;

    @Column(name = "password")
    String password;

    @Column(name = "first_name")
    String firstName;

    @Column(name = "last_name")
    String lastName;
    
    public User(String username, String password, String firstName, String lastName) {
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getUserId() {
        return id;
    }

    public void setUserId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

App.java:

package org.studyeasy.hibernate;

import org.hibernate.Session;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.studyeasy.hibernate.entity.User;

public class App {

    public static void main(String[] args) {
        SessionFactory factory = new    Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(User.class)
                .buildSessionFactory();

        Session session = factory.getCurrentSession();

        try {
            // Create object of entity class type
            User user = new User("username", "password", "firstName", "lastName");
            // Start transaction
            session.beginTransaction();
            // Perform operation
            session.save(user);
            // Commit the transaction
            session.getTransaction().commit();
            System.out.println("Row added!");
        } finally {
            session.close();
            factory.close();
        }
    }
}

xml文件:

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
    <session-factory>
 
        <!-- Connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
              <!-- Sample MySQL URL provided  -->  
        <property name="connection.url">jdbc:mysql://localhost:3306/project</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
           
        <!-- Show SQL on console -->
        <property name="show_sql">true</property>
 
        <!--Setting Session context model -->
        <property name="current_session_context_class">thread</property>
 
    </session-factory>
</hibernate-configuration>

完整错误:

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:87)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:123)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:118)
    at org.studyeasy.hibernate.App.main(App.java:12)
     Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 5 more

我将jaxb-api-23.1.jar添加到lib文件夹和build-path中,但我收到以下错误:

Jan 05, 2023 12:46:58 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.8.Final}
Jan 05, 2023 12:46:58 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
    at org.studyeasy.hibernate.App.main(App.java:13)
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)
    ... 5 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
    at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
    ... 9 more
isr3a4wc

isr3a4wc1#

当您使用java 9或更高版本的java时会发生此错误。
JAXB history开始的Java上JAXB的简史雅加达XML绑定(JAXB;以前的Java Architecture for XML Binding)是一个XML绑定框架,用于将Java类转换为XML或将XML转换为Java类。

  1. JAXB是Java 6、7和8的一部分。
  2. Java 9不支持Java EE模块,包括JAXB包javax.xml。
  3. JAXB API在Java 10中仍然被弃用。
  4. Java 11完全删除了JAXB API javax.xml. *。
  5. Oracle将Java EE提交给Eclipse基金会,并从3.0版开始将其重新打包为jakarta.xml。
    在这里,您可以找到一个定义良好的解决方案javax/xml/bind/JAXBException error

相关问题