Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 185763
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T06:14:05+00:00 2022-06-10T06:14:05+00:00

java – Getting: ‘Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]’ Exception when doing Hibernate CRUD project

  • 0

[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]

  • 0 0 Answers
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 324 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 295 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 291 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.