8

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)
UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
In-young Choung
  • 779
  • 2
  • 8
  • 22

7 Answers7

8

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>
Dherik
  • 17,757
  • 11
  • 115
  • 164
UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
4

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

Kapil Thakkar
  • 840
  • 10
  • 17
3
 <property name="connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>

You need to set serverTimezone.

4b0
  • 21,981
  • 30
  • 95
  • 142
jingyuan iu
  • 93
  • 1
  • 1
2

Use HSQLDialect not MYSQLDialect

spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true


spring.datasource.url=jdbc:mysql://localhost:3306/xyz?serverTimezone=CST6CDT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
Beginner
  • 41
  • 4
1

You need to add mysql JDBC jar in your dependencies.


  1. Fix your driver class name as com.mysql.jdbc.Driver.
  2. Fix your username and password property as

    "connection.username" for database user
    "connection.password" for database user password
    
  3. Create mysql database. See this.

  4. For SSL warning, modify your connection.url to include use ssl false. For example, jdbc:mysql://localhost:3306/<enter-your-database>?autoReconnect=true&useSSL=false

  5. Modify your mysql dialect to org.hibernate.dialect.MySQLDialect

  6. Use <property name="hbmdl.auto">create-drop</property> instead of <property name="hbmdl.auto">update</property> however do not use this option in production. You should create schema yourself and not do it via hibernate.
GauravJ
  • 2,162
  • 1
  • 22
  • 28
  • I added mysql mysql-connector-java 5.1.40 I still get the same error. – In-young Choung Mar 13 '17 at 17:25
  • Can you also fix your driver class name as ` com.mysql.jdbc.Driver` – GauravJ Mar 13 '17 at 17:28
  • I changed the driver class. I get this error. Mon Mar 13 13:31:53 EDT 2017 WARN: Establishing SSL connection without server's identity verification is not recommended . According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore f or server certificate verification. – In-young Choung Mar 13 '17 at 17:33
  • and, Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate .engine.jdbc.env.spi.JdbcEnvironment] – In-young Choung Mar 13 '17 at 17:33
  • See my modified answer – GauravJ Mar 13 '17 at 17:40
  • Thank you so much! When I include ?autoReconnect = true & useSSL = false, I get a compile error; Java compiler level does not match the version of the installed Java project facet. – In-young Choung Mar 13 '17 at 17:46
  • check http://stackoverflow.com/questions/7715260/java-compiler-level-does-not-match-the-version-of-the-installed-java-project-fac – GauravJ Mar 13 '17 at 17:49
  • based on the stackoverflow, I've added the code in the pom file maven-compiler-plugin 1.8 1.8 It still doesn't work.. – In-young Choung Mar 13 '17 at 18:04
  • I have modified my answer (dialect was wrong). Check if it works. – GauravJ Mar 13 '17 at 18:07
  • Thank you! For creating database #3, is that optional? I understood that JPA will auto generate the db tables using mapped annotations. – In-young Choung Mar 13 '17 at 18:30
  • I might have misunderstood what you meant by creating database. I had already created database. (I included database name in URL config now) I just want to successfully create tables.. I've fixed many issues thanks to your help, but I still get the same JDBC environment error. – In-young Choung Mar 13 '17 at 19:34
  • Can you share your hibernate.cfg and console logs again ? I have added another point in my answer. – GauravJ Mar 14 '17 at 06:07
0

I had the same issue when I deployed my war file to tomcat in my demo environment. My application was not getting started because, in my /tomcat/webapp/ROOT folder, I found my war file Myapp.war there. i.e. In my ROOT folder, it has WEB-INF, META-INF, and Myapp.war, which caused the startup issue. I deleted the war file and it worked.

MishraJi
  • 304
  • 2
  • 6
  • 18
0

I had the same problem: the mistake I was making was having a double line defined between the properties. So you have to be very careful that the main connection properties are correct.

Secondly, make sure that the specified dialect is correct for the DB version you are using. To see the various dialects you need to go to the hibernate-core-5.3.1.Final.jar file and from there look for the org.hybernate.dialect package which lists the various dialects available.

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- property name="connection.driver_class">com.mysql.jdbc.Driver</property  com.mysql.cj.jdbc.Driver -->
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">mypassword</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

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

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

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

        <property name="current_session_context_class">thread</property>

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

        <!-- Names the annotated entity class -->
        <mapping class="org.test.connection.UserDetails" />

    </session-factory>

</hibernate-configuration>