1

I'm learning spring framework. While learning I was working on Spring-orm with Hibernate. I'm trying to connect to the database and do some CRUD operation, but I'm facing this issue.

ERROR: HHH000302: Unable to construct current session context [org.springframework.orm.hibernate5.SpringSessionContext]
org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.springframework.orm.hibernate5.SpringSessionContext]
Caused by: java.lang.NoClassDefFoundError: javax/transaction/SystemException
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:467)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:120)
Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException
    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)

I'm using Eclipse IDE and I ran a main method in App.java class as a Java Application to test the CRUD operation.I downloaded the latest version of both Spring and Hibernate packages and some additional ones from Maven Central. My current pom.xml dependencies look like this:

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.19</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.19</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>5.3.19</version>
  </dependency>

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

  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.29</version>
  </dependency>

  <dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.2.Final</version>
  </dependency>
</dependencies>

I configured my spring configuration to work on my local instance of MySQL. This is the configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            ">
  <tx:annotation-driven />
  <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="ds">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/mydb"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
  </bean>
  <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" name="factory">
    <property name="dataSource" ref="ds"></property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update </prop>
      </props>
    </property>
    <property name="annotatedClasses">
      <list>
        <value>
          org.sb.retry.springorm.springorm1.entities.Student
        </value>
      </list>
    </property>
  </bean>
  <bean class="org.springframework.orm.hibernate5.HibernateTemplate" name="hibernateTemplate">
    <property name="sessionFactory" ref="factory"></property>
  </bean>
  <bean class="org.sb.retry.springorm.springorm1.dao.StudentDAO" name="studentDao">
    <property name="hibernateTemplate" ref="hibernateTemplate">
    </property>
  </bean>
  <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" name="transactionManager">
    <property name="sessionFactory" ref="factory"></property>
  </bean>
</beans>

I know my database credentials are right because Hibernate created the table using my entity but was unable to perform CRUD operations on it.

This is my Entity:

package org.sb.retry.springorm.springorm1.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "student_details")
public class Student {
    @Id
    @Column(name = "student_id")
    private int id;
    @Column(name = "student_name")
    private String name;
    @Column(name = "student_city")
    private String city;

    public Student(int id, String name, String city) {
        super();
        this.id = id;
        this.name = name;
        this.city = city;
    }

    public Student() {
        super();
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}

This is the DAO class where I implement HibernateTemplate and other hibernate methods using it.

package org.sb.retry.springorm.springorm1.dao;

import org.sb.retry.springorm.springorm1.entities.Student;
import org.springframework.orm.hibernate5.HibernateTemplate;

import jakarta.transaction.Transactional;

public class StudentDAO {
    private HibernateTemplate hibernateTemplate;

    // save student
    @Transactional
    public int insert(Student student) {
        Integer res = (Integer) this.hibernateTemplate.save(student);
        return res;
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
    
}

Please help me troubleshoot this issue. I'm not able to find any concrete solutions to this problem after 5 hours of digging through stack overflow threads.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

1

If you are just starting with Spring and Hibernate, I would advise you to downgrade your hibernate to version 5. It is a common pitfall for people to jump into the latest versions, and they end up being blocked instead of learning.

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.23.Final</version>
        </dependency>

Also keep in mind that if you are using JDK11 or above, it should be the 5.4 or above version of hibernate, while on another side if you are using JDK8 it will work with anything.

The latest is never the greatest.

Also, make sure you have @GeneratedValue under your @Id annotation in your entity

@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column(name = "student_id")
private int id;

List of JDK8+ dependencies

<!-- https://mvnrepository.com/artifact/com.sun.activation/javax.activation -->
<dependency>
    <groupId>com.sun.activation</groupId>
    <artifactId>javax.activation</artifactId>
    <version>1.2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.0</version>
</dependency>

After that you can just right click on your project > Maven > Update.

zawarudo
  • 1,907
  • 2
  • 10
  • 20
  • Okay thank I'm trying.. I have jdk 17. and I'm downgrading hibernate core to 5.6.5.FINAL and spring framework to 5.2.3.RELEASE – Soumalya Bhattacharya May 09 '22 at 13:19
  • if you are getting some noclassfound jaxb exceptions these are the jars/dependecies that you will have to include javax.activation-1.2.0.jar jaxb-api-2.3.0.jar jaxb-core-2.3.0.jar jaxb-impl-2.3.0.jar This is because after JDK8 these jars were removed from JDK, and they need to be added manually. – zawarudo May 09 '22 at 13:20
  • I was actually getting something like that. Please tell me how can I add these jars in Eclipse – Soumalya Bhattacharya May 09 '22 at 13:25
  • everything is located on maven central website you just need to find proper versions, let me check and I will update my answer – zawarudo May 09 '22 at 13:28
1

I had similar problem. Using JDK11 I had to downgrade hibernate-core to 5.4.23.Final, spring-framework to 5.3.21 and everything works fine.

Below my properties.

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <springframework.version>5.3.21</springframework.version>
    <hibernate.version>5.4.23.Final</hibernate.version>
    <jackson.version>2.13.3</jackson.version>
    <javax.servlet.version>4.0.1</javax.servlet.version>
    <javax.annotation.version>1.3.2</javax.annotation.version>
    <mysql.connector.version>8.0.29</mysql.connector.version>
    <c3p0.version>0.9.5.5</c3p0.version>
    <javax.servlet.jsp.version>2.3.3</javax.servlet.jsp.version>
    <javax.xml.bind.version>2.3.0</javax.xml.bind.version>
  </properties>
Sloma44
  • 11
  • 2