无法创建请求的服务[org.hibernate .engine.jdbc.env.spi.JdbcEnvironment] -MySQL [英] Unable to create requested service [org.hibernate .engine.jdbc.env.spi.JdbcEnvironment]-MySQL

查看:105
本文介绍了无法创建请求的服务[org.hibernate .engine.jdbc.env.spi.JdbcEnvironment] -MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate的新手.我目前正在使用Spring引导框架,并试图通过hibernate创建数据库表.

I am a newbie to Hibernate. I am currently using Spring boot framework and trying to create database tables through hibernate.

我知道之前也问过同样的问题,但是我似乎无法根据我的环境弄清楚该错误的解决方法.

I know the same question is asked before but I can't seem to figure out how to fix the error based on my environment.

hibernate.cfg.xml

hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.mm.mysql.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306</property>
    <property name="connection_userid">user</property>
    <property name="connection_pwd">pass</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection_pool_size">true</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.MySQLDialect</property>

    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">1</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbmdl.auto">update</property>

    <!-- Names the annotated entity class -->
    <mapping class="com.test.springboot.model.AdultParticipant" />

</session-factory>

主班

     public static void main(String[] args) throws Exception {
    SpringApplication.run(WebApplication.class, args);

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    AdultParticipant ap = new AdultParticipant();
    ap.setFirstName("User"); 
    ap.setLastName("UserLastName");

    session.persist(ap);

    t.commit();

    session.close();
    System.out.println("successfully saved");

POJO类

@Entity
@Table(name = "adultparticipant")
public class AdultParticipant {

@GeneratedValue
@Id
@Column (name = "id")
private int id;

@Column (name = "firstName")
private String firstName;

@Column (name = "lastName")
private String lastName;


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;
  }
}

DAOImpl类

  public class AdultParticipantDAOImpl implements AdultParticipantDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

@Override
public void save(AdultParticipant ap) {
    Session session = this.sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.persist(ap);
    tx.commit();
    session.close();
}

@SuppressWarnings("unchecked")
@Override
public List<AdultParticipant> list() {
    Session session = this.sessionFactory.openSession();
    List<AdultParticipant> adultParticipants = session.createQuery("from AdultParticipant").list();
    session.close();
    return adultParticipants;
 }
}

DAO类

public interface AdultParticipantDAO {

public void save(AdultParticipant p);

public List<AdultParticipant> list();

}

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>
<artifactId>hello-springboot</artifactId>
<name>hello-springboot</name>
<description>hello-springboot</description>
<packaging>war</packaging>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

控制台错误

2017-03-13 11:48:40.512  WARN 9532 --- [           main] org.hibernate.orm.connections.pooling    : HHH10001002: Using H
ibernate built-in connection pool (not for production use!)
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate
.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)

推荐答案

将MySql驱动程序升级到 mysql-connector -java-8.0.17

Upgrade MySql driver to mysql-connector-java - 8.0.17 and

正在使用 大于MySQL 5.5 的用户版本

Those who are using greater than MySQL 5.5 version

更改其驱动程序属性

com.mysql.jdbc.Driver com.mysql.cj.jdbc.Driver

from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver

因为:

正在加载com.mysql.jdbc.Driver类.不推荐使用.新的 驱动程序类是com.mysql.cj.jdbc.Driver.驱动程序自动 通过SPI注册并手动加载驱动程序类 通常是不必要的.INFO-HHH000401:使用驱动程序 URL上的[com.mysql.jdbc.Driver]....

Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.INFO - HHH000401: using driver [com.mysql.jdbc.Driver] at URL....

hibernate.properties

in hibernate.properties

hibernate.connection.driver_class = com.mysql.cj.jdbc.Driver

(如果您使用的是 hibernate.cfg.xml 更新

or if you are using hibernate.cfg.xml update

<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>

这篇关于无法创建请求的服务[org.hibernate .engine.jdbc.env.spi.JdbcEnvironment] -MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆