简体   繁体   中英

I am using hibernate, no pom.xml or maven and getting the error?

This is my main class

package com.luv2code.hibernate.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class CreateStudentDemo {

    public static void main(String[] args) {
        
        // create session factory
        SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Student.class).buildSessionFactory();
        
        // create session
        Session session = factory.getCurrentSession();
        try {
            
            // create stydent object
            System.out.println("Creating a new student object");
            Student tempStudent = new Student("Paul", "Wall", "paulWall@gmail.com");
            
            // start a transaction
            session.beginTransaction();
            
            // save student object
            System.out.println("Saving student ");
            session.save(tempStudent);
            
            // commit the transaction 
            session.getTransaction().commit();
            System.out.println("Done ...");
            
            
        }
        catch (Exception e) {
            System.out.println("ERROOOOORRR");
        }finally {
            ((SessionFactory) factory).close();
        }
    }

}

This is my hibernate configuration file

<!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>

        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&amp;serverTimezone=UTC</property>
        <property name="connection.username">hbstudent</property>
        <property name="connection.password">hbstudent</property>

        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">1</property>

        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Set the current session context -->
        <property name="current_session_context_class">thread</property>
 
    </session-factory>

</hibernate-configuration>

this is the error I get

Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException at org.hibernate.boot.spi.XmlMappingBinderAccess.(XmlMappingBinderAccess.java:43) at org.hibernate.boot.MetadataSources.(MetadataSources.java:87) at org.hibernate.cfg.Configuration.(Configuration.java:123) at org.hibernate.cfg.Configuration.(Configuration.java:118) at com.luv2code.hibernate.demo.CreateStudentDemo.main(CreateStudentDemo.java:13) Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Bu iltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)... 5 more

Java removed java.xml.bind from JAVA 9 and higher editions. Here is the solution for same kind of issues:

Intellij: https://stackoverflow.com/a/73766451/18263761

Eclipse: https://stackoverflow.com/a/73766498/18263761

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM