0

I am learning to hibernate so I made a small app for crud operation using mySQL as database. However, I am getting some errors and I cannot find the solution anywhere. SDK 17.0.2, I am not using maven , Also all hibernate final jar files have been added

my class:

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

import com.luv2code.hibernate.demo.entity.Student;

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 a Student object
            System.out.println("Creating new student");
            Student tempStudent = new Student("Pau;", "Wall", "paul@luv2code.com");
            
            //Start a transaction
            session.beginTransaction();
            
            //save the student object
            session.save(tempStudent);
            
            
            //commit transaction
            session.getTransaction().commit();
        }
        finally {
            factory.close();
        }

    }

}

runtime error :

java.security.PrivilegedActionException: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain) 
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.reflect.Method.invoke(Object, Object[])" because "com.sun.xml.bind.v2.runtime.reflect.opt.Injector.defineClass" is null
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • `sun.misc.Unsafe.defineClass` was removed around Java 11, so either find a newer version of the software that supports Java 17 or run with Java 8. – greg-449 Mar 23 '22 at 10:47

2 Answers2

0

java removed java.xml.bind from JAVA 9 and higher editions. You need to add these jar files manually to your library basically you can to that in this way. If you use Intellij this is the easyest way:

jaxb-impl-2.3.0.jar

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.0</version>
</dependency>

jaxb-core-2.3.0.jar

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0</version>
</dependency>

jaxb-api-2.3.1.jar

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

javax.activation-api-1.2.0.jar

<dependency>
    <groupId>com.sun.activation</groupId>
    <artifactId>javax.activation</artifactId>
    <version>1.2.0</version>
</dependency>

If you use intellij here is a step my step to get these files through IDE

  1. go to project structure:

enter image description here

  1. Libraries, and click + sign

enter image description here

  1. Chick to from Maven:

enter image description here

  1. Paste these dependencies to the search.

enter image description here

It will automatically find exact jar files to download, Kist Paste it and click to the enter that is it.

Also later you can just add these files to the classpath just in case:

enter image description here

ilia
  • 98
  • 1
  • 8
0

java removed java.xml.bind from JAVA 9 and higher editions. Here is the eclipse solution:

In Eclipse IDE:

  1. Download these jar files and paste them inside lib folder:

enter image description here

  1. Go the project properties: The last line

enter image description here

  1. Done these steps:

enter image description here

Happy Coding! :)

ilia
  • 98
  • 1
  • 8