Hibernate 5 - Basic configuration example

In this post, we will learn how to create or configure a simple hibernate application to connect MySQL database.

Technologies and tools used for this application are -

  • Hibernate 5.2.6.Final
  • Eclipse Mars.2 (4.5.2)
  • Maven 3.3.9
  • JavaSE 1.8
  • MySQL 5.7.12


Project Structure

Review the following project structure.

So let us proceed to write a simple application, which will print the version of the MySQL database.

Step 1 – Create a maven project

Refer this article to learn - How to create a maven project in eclipse.


Step 2 - Add jar dependencies to pom.xml

Open pom.xml file and add required Hibernate and MySQL connector dependencies to it. Here is the pom.xml file.


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




    <!-- MySQL connector -->
    <!-- Hibernate 5.2.6 Final -->



Step 3 - Create a Hibernate configuration file

Create an XML file named as hibernate.cfg.xml under resources folder and write the following code in it.


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"

    <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/BORAJI</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


Step 4 - Create a Hibernate utility class

Create a class named as HibernateUtil.java and write the following code in it.


package com.boraji.tutorial.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

 * @author imssbora
public class HibernateUtil {
  private static StandardServiceRegistry registry;
  private static SessionFactory sessionFactory;

  public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
      try {
        // Create registry
        registry = new StandardServiceRegistryBuilder()

        // Create MetadataSources
        MetadataSources sources = new MetadataSources(registry);

        // Create Metadata
        Metadata metadata = sources.getMetadataBuilder().build();

        // Create SessionFactory
        sessionFactory = metadata.getSessionFactoryBuilder().build();

      } catch (Exception e) {
        if (registry != null) {
    return sessionFactory;

  public static void shutdown() {
    if (registry != null) {


Step 5 - Create a main class and run application

Here is the main class to check the database version.


package com.boraji.tutorial.hibernate;

import org.hibernate.Session;

 * @author imssbora
public class MainApp {
  public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().openSession();

    // Check database version
    String sql = "select version()";

    String result = (String) session.createNativeQuery(sql).getSingleResult();





You can download this application from the below link..