[ad_1]
I am doing a ‘JAVA project’ in Eclipse Enterprise Edition IDE. Hibernate CRUD project to create user (student) details in my MySQL database (hibernatestudent). Hibernate version is Hibernate ORM core version 5.6.9.Final. My MySQL version is 8.0.29. I have the same version connector jar file for JDBC added in the build path library. All other related jar files related to Hibernate have been added also. I have tried the suggested solutions from other such issues of users but none seems to solve my problem.
Below shows Exception when running StudentDAO.java file.
Exception
Jun 10, 2022 11:46:40 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 5.6.9.Final
Jun 10, 2022 11:46:40 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
POJO Class file
package com;
// POJO Class file (persistent class)
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Student")
public class Student {
@Id // Primary key for this table.
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
private String firstName;
private String lastName;
private String email;
public int getId() { // Generating getters & setters.
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;
}
}
StudentDAO.java file
package com;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class StudentDao {
public static void main(String[] args) { // Main method
StudentDao.insert_data();
}
public static void insert_data() {
try {
// Hibernate API to save this objects to DB
//Session factory is created only ONCE
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
// create transaction
session.beginTransaction();
Student obj_Student = new Student(); // Sending the above collected values to this entity class to be stored in our database.
obj_Student.setFirstName("Student_One");
obj_Student.setId(987);
session.save(obj_Student);
session.getTransaction().commit();
//Closing the session
session.close();
sessionFactory.close();
}catch (Exception e) {
System.out.println(e);
}
}
}
hibernate cfg xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Version 8 MySQL hibernate.cfg.xml for Hibernate 5 -->
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.datasource">jdbc/myDS</property>
<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">javax.persistence.jdbc.url=jdbc:mysql://localhost:3306/hibernatestudent</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<!-- Disable auto commit mode -->
<property name="hibernate.connection.autocommit">false</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">4</property>
<property name="current_session_context_class">thread</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Format SQL -->
<property name="format_sql">true</property>
<!-- Database tables are automatically created -->
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- Names the annotated entity class-->
<mapping class ="com.Student"/>
</session-factory>
</hibernate-configuration>
jar files used are as below
jar files
[ad_2]