I am writing a simple program to test and learn Hibernate ORM with Gradle 8.0 on Apache NetBeans 17. I will use H2 DB which can be used as an embedded database.
The program I write will run on computers with Java 8 installed, so I have to use the latest Hibernate version compatible with Java 8. Therefore, the technology versions I have chosen are listed below:
Main.java
import java.util.List;
import org.mypackage.database.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Main {
public static void main(String[] args) {
Student student = new Student("John", "Cena", "johncena@blablabla.com");
Transaction transaction = null;
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
System.out.println("INFO: Session opened");
transaction = session.beginTransaction();
session.save(student);
transaction.commit();
} catch (Exception e) {
System.out.println("ERROR" + e.getMessage());
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
List < Student > students = session.createQuery("from Student", Student.class).list();
students.forEach(s -> System.out.println(s.getFirstName()));
} catch (Exception e) {
System.out.println("ERROR" + e.getMessage());
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
}
}
Student.java
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "student")
public class Student{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
public Student() {
}
public Student(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}
}
HibernateUtils.java
package org.mypackage.database;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class HibernateUtil {
private static StandardServiceRegistry registry;
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().build();
try {
sessionFactory = new MetadataSources(registry)
.addAnnotatedClass(Student.class)
.buildMetadata()
.buildSessionFactory();
}
catch (Exception e) {
StandardServiceRegistryBuilder.destroy(registry);
}
} catch (Exception e) {
e.printStackTrace();
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
return sessionFactory;
}
public static void shutdown() {
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
hibernate.properties
# Database connection settings
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.pool_size=1
hibernate.dialect=org.hibernate.dialect.H2Dialect
# Echo all executed SQL to console
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.highlight_sql=true
hibernate.hbm2ddl.auto=create-drop
# Automatically export the schema
hibernate.current_session_context_class=thread
hibernate.hbm2ddl.auto=create
hibernate.dbcp.initialSize=5
hibernate.dbcp.maxTotal=20
hibernate.dbcp.maxIdle=10
hibernate.dbcp.minIdle=5
hibernate.dbcp.maxWaitMillis=-1
build.gradle
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath 'com.github.johnrengelman:shadow:8.1.1'
}
}
plugins {
id 'com.github.johnrengelman.shadow' version '8.1.1'
id 'java'
id 'application'
}
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
mainClassName = 'org.mypackage.Main'
group = 'org.mypackage'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.hibernate.orm:hibernate-java8:6.0.0.Alpha7'
implementation 'com.h2database:h2:2.2.224'
implementation 'jakarta.persistence:jakarta.persistence-api:3.0.0'
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
test {
useJUnitPlatform()
}
compileJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
I am working with the code I found on the internet. However, although everything is correct, I get the following error. I've been searching for a few days but nothing has changed, I didn't even get a different error. I'm writing here as a last resort. if there is any other information you want me to add, please indicate in your post.
As mentioned above by @MarkRotteveel, my problem was solved when I used Hibernate 5.6.15.Final and Jakarta 2.3.3. I'm marking this post as a solution to help others.