0

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(BuiltinClassLoader.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

  • Jaxb package not included jdk package solution explain here https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception – sjy Aug 04 '22 at 00:16
  • 1
    Does this answer your question? [How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException](https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception) – AddeusExMachina Aug 04 '22 at 07:11
  • The problem was in the jdk. I set up a new project specifying jdk 1.8 as default jdk and imported the necessary jars and worked fine for me. Thank you for help – Mohammed Abdou TOUNSSI Aug 07 '22 at 15:01

1 Answers1

0

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

ilia
  • 98
  • 1
  • 8