An example hibernate.cfg.xml for MySQL 8 and Hibernate 5

There’s a recent article on TheServerSide that hopefully reminded Hibernate and JPA developers that they’re using the correct Hibernate dialect and database drivers in their persistence.xml files.

There are plenty of applications in production that use the Hibernate framework exclusively without JPA. That is why I figured it would also be helpful to provide an example hibernate.cfg.xml for MySQL 8 using classes found in Hibernate 5 distributions.

Hibernate MySQL dialects

The latest Hibernate 5 release includes updated dialect classes that must be referenced within a hibernate.cfg.xml file for MySQL 8.

<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>

There are a number of different dialects that could potentially be specified in the hibernate.cfg.xml for MySQL The correct dialect to choose depends upon the MySQL version in use and the type of database engine in play, like MyISAM or InnoDB. If a developer fails to choose the correct dialect, it will result in unpredictable Hibernate query results at runtime.

Correct MySQL drivers for Hibernate

The other update a modern MySQL hibernate.cfg.xml file requires is a change to the JDBC driver class.

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

This driver isn’t part of the Hibernate distribution but is instead delivered as part of the JDBC drivers package available from the MySQL website.

Example hibernate.cfg.xml for MySQL

When a developer puts it all together, an example MySQL hibernate.cfg.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
<!-- Version 8 MySQL hiberante-cfg.xml example for Hibernate 5 -->
    <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <!-- property name="connection.driver_class">com.mysql.jdbc.Driver</property -->
    <property name="connection.url">jdbc:mysql://localhost/database</property>
    <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
    <property name="connection.username">root</property>
    <property name="connection.password">password</property>
    <property name="connection.pool_size">3</property>
    <!--property name="dialect">org.hibernate.dialect.MySQLDialect</property-->
    <property name="current_session_context_class">thread</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="">update</property>
    <!-- mapping class="com.mcnz.jpa.examples.Player" / -->

This MySQL hibernate.cfg.xml example includes optional settings to generate and format SQL as well as an instruction to create database tables if any entity beans require them.

You can find the source code for a working Hibernate web application that uses this example hibernate.cfg.xml for MySQL on my GitHub account.

App Architecture
Software Quality
Cloud Computing