异常java.lang.NoClassDefFoundError:javax/xml/bind/JAXBException在一个简单的maven hibernate项目中使用mysql

5m1hhzi4  于 2023-04-06  发布在  Java
关注(0)|答案(1)|浏览(194)

我正在intellij idea中使用maven,hibernate和mysql进行一个项目。这是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>tsp3</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>tsp3</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.sourse>1.8</maven.compiler.sourse>
    <maven.compiler.target>1.8</maven.compiler.target>
    <hibernate-version>5.0.1.Final</hibernate-version>
  </properties>

  <dependencies>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.0.1.Final</version>
    </dependency>

    <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.0.1.Final</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.32</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-tools</artifactId>
      <version>4.3.2.Final</version>
    </dependency>

  </dependencies>
</project>

这是我的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306</property>
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">170302</property>
        <property name="show_sql">true</property>
        <mapping class="org.example.MessegesEntity"/>
        <mapping resource="mappings/MessegesEntity.hbm.xml"/>
        <mapping resource="mappings/ThemeEntity.hbm.xml"/>
        <mapping class="org.example.ThemeEntity"/>
        <mapping resource="mappings/UsersEntity.hbm.xml"/>
        <mapping class="org.example.UsersEntity"/>
        <!-- <property name="connection.username"/> -->
        <!-- <property name="connection.password"/> -->

        <!-- DB schema will be updated if needed -->
        <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

这是我的SessionFactory类

import org.example.MessegesEntity;
import org.example.ThemeEntity;
import org.example.UsersEntity;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactoryUtil {

    private static SessionFactory sessionFactory;

    private HibernateSessionFactoryUtil() {}

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                Configuration configuration = new Configuration().configure();
                configuration.addAnnotatedClass(MessegesEntity.class);
                configuration.addAnnotatedClass(UsersEntity.class);
                configuration.addAnnotatedClass(ThemeEntity.class);
                StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
                sessionFactory = configuration.buildSessionFactory(builder.build());

            } catch (Exception e) {
                System.out.println("Exception!" + e);
            }
        }
        return sessionFactory;
    }
}

我有数据库和Map。我也有类,DAO和服务类为我的表。我试图创建新的用户,并将其保存到我的数据库在我的主方法。第一个异常出现在HibernateSessionFactoryUtil类在配置configuration = new Configuration().configure();
我所有的例外:

jdk版本20?

我将感谢您的帮助!

kpbwa7wx

kpbwa7wx1#

我把它添加到我的pom.xml

<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.3.1</version>
</dependency>

相关问题