【发布时间】:2022-11-10 03:49:26
【问题描述】:
我在 Eclipse Enterprise Edition IDE 中做一个“JAVA 项目”。 Hibernate CRUD 项目在我的 MySQL 数据库 (hibernatestudent) 中创建用户(学生)详细信息。 Hibernate 版本是 Hibernate ORM 核心版本 5.6.9.Final。我的 MySQL 版本是 8.0.29。我在构建路径库中添加了相同版本的 JDBC 连接器 jar 文件。还添加了与 Hibernate 相关的所有其他相关 jar 文件。我已经尝试了其他此类用户问题的建议解决方案,但似乎没有一个可以解决我的问题。
下面显示了运行 StudentDAO.java 文件时的异常。
例外
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 类文件
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 文件
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);
}
}
}
休眠cfg xml文件
<?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文件如下jar files
【问题讨论】:
标签: java mysql eclipse hibernate jdbc