首页 > 解决方案 > 错误:无法使用 Java 和 MySQL 创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

问题描述

我是 Hibernate 新手,在填充数据库时遇到问题:

 <?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">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/***?serverTimezone=UTC</property>
        <property name="hibernate.connection.username">***</property>
        <property name="hibernate.connection.password">***</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create </property>
        <mapping resource="com/jwt/hibernate/bean/user.hbm.xml" />
    </session-factory>
</hibernate-configuration>

我的用户资源映射如下:

  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.org/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.jwt.hibernate.bean.User" table="USER">
        <id column="ID" name="id" type="java.lang.Integer" />
        <property column="USER_NAME" name="userName" type="java.lang.String" />
        <property column="PASSWORD" name="password1" type="string" />
        <property column="EMAIL" name="email" type="java.lang.String" />
        <property column="PHONE" name="phone" type="java.lang.String" />
        <property column="CITY" name="city" type="java.lang.String" />
    </class>
</hibernate-mapping>

这是我配置休眠的类

package com.jwt.hibernate.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.jwt.hibernate.bean.User;

public class UserDAO {

    public void addUserDetails(String userName, String password, String email,
                               String phone, String city) {
        try {
            // 1. configuring hibernate
            Configuration configuration = new Configuration().configure();

            // 2. create sessionfactory
            SessionFactory sessionFactory = configuration.buildSessionFactory();

            // 3. Get Session object
            Session session = sessionFactory.openSession();

            // 4. Starting Transaction
            Transaction transaction = session.beginTransaction();
            User user = new User();
            user.setUserName(userName);
            user.setPassword1(password);
            user.setEmail(email);
            user.setCity(city);
            user.setPhone(phone);
            session.save(user);
            transaction.commit();
            System.out.println("\n\n Details Added \n");

        } catch (HibernateException e) {
            System.out.println(e.getMessage());
            System.out.println("error");
        }

    }

}

当我试图编译请求表时,我总是会收到这个错误:

08-Jul-2020 16:46:50.585 WARN [http-nio-8080-exec-11] org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver.resolveEntity HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
08-Jul-2020 16:46:50.688 WARN [http-nio-8080-exec-11] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
error

这是我的 Maven pom.xml

  <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>WebApplication</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.14</maven.compiler.source>
        <maven.compiler.target>1.14</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-agroal</artifactId>
            <version>5.4.18.Final</version>
            <type>pom</type>
        </dependency>


        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>

    </dependencies>


    
</project>

我在兼容性方面遇到问题吗?我在这里尝试了所有解决方案,但没有任何改变。谢谢

标签: javahibernatemaven

解决方案


推荐阅读