News Stay informed about the latest enterprise technology news and product updates.

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

We recently pushed up an article reminding Hibernate and JPA developers to ensure they are using the correct Hibernate dialect and database drivers in their persistence.xml files. Given the fact that there are plenty of application in production that use the Hibernate framework exclusively without JPA, I figured it would be a kind gesture to also 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 references within a hibernate.cfg.xml file for MySQL 8.

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

It’s worth noting that there are a number of different dialects that could potentially to 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, be it MyISAM or InnoDB. Failing to choose the correct dialect 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 is not 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 you put it all together, an example MySQL hibernate.cfg.xml file looks like this. Note that this MySQL hibernate.cfg.xml example includes extra settings to generate and format SQL as well as an instruction to create database tables if any entity beans require them. These are optional settings.

<?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">
<!-- Version 8 MySQL hiberante-cfg.xml example for Hibernate 5 -->
<hibernate-configuration>
  <session-factory>
    <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="hbm2ddl.auto">update</property>
    <!-- mapping class="com.mcnz.jpa.examples.Player" / -->
  </session-factory>
</hibernate-configuration>

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.

 

 

 

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchAppArchitecture

SearchSoftwareQuality

SearchCloudComputing

SearchSecurity

SearchAWS

Close