Issue
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 Enttiy 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.
Solution
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.
Answered By - zawarudo
Answer Checked By - Timothy Miller (JavaFixing Admin)