Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create requested service [org.hibernate .engine.jdbc.env.spi.JdbcEnvironment]-MySQL

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

main class

     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 class

@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 class

  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 class

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>

ERROR in Console

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)
like image 312
In-young Choung Avatar asked Mar 13 '17 16:03

In-young Choung


3 Answers

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

Those who are using greater than MySQL 5.5 version

change their driver property

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

because:

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....

in hibernate.properties

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

or if you are using hibernate.cfg.xml update

<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
like image 139
UdayKiran Pulipati Avatar answered Oct 27 '22 00:10

UdayKiran Pulipati


connection.driver_class property must be compatible with mysql-connector jar.

mysql-connector < 5.5.xx use com.mysql.jdbc.Driver

mysql-connector > 5.5.xx use com.mysql.cj.jdbc.Driver

like image 22
Kapil Thakkar Avatar answered Oct 26 '22 23:10

Kapil Thakkar


 <property name="connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>

You need to set serverTimezone.

like image 27
jingyuan iu Avatar answered Oct 27 '22 01:10

jingyuan iu