Hibernate 5 - Basic configuration example without hibernate.cfg.xml

Hibernate 5 - Basic configuration example

In this post, we will show you how to create or configure a simple Hibernate application without using hibernate.cfg.xml.

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
  • PostgreSQL 9.6.1


Project Structure

Review the following project structure.

Let us proceed to write a simple application, which will print the version of the PostgreSQL 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 PostgreSQL dependencies to it.


<project xmlns="http://maven.apache.org/POM/4.0.0" 




    <!-- PostgreSQL  -->
    <!-- Hibernate 5.2.6 Final -->



Step 3 - 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 java.util.HashMap;
import java.util.Map;

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;
import org.hibernate.cfg.Environment;

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

   public static SessionFactory getSessionFactory() {
      if (sessionFactory == null) {
         try {

            // Create registry builder
            StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();

            // Hibernate settings equivalent to hibernate.cfg.xml's properties
            Map<String, String> settings = new HashMap<>();
            settings.put(Environment.DRIVER, "org.postgresql.Driver");
            settings.put(Environment.URL, "jdbc:postgresql://localhost:5432/BORAJI");
            settings.put(Environment.USER, "postgres");
            settings.put(Environment.PASS, "admin");
            settings.put(Environment.DIALECT, "org.hibernate.dialect.PostgreSQL9Dialect");

            // Apply settings

            // Create registry
            registry = registryBuilder.build();

            // 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) {

As you can see in the above example, the applySettings() method of the  StandardServiceRegistryBuilder class is used for loading configuration details, required for bootstrapping a Hibernate application. 


Step 4 - 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();




PostgreSQL 9.6.1, compiled by Visual C++ build 1800, 64-bit

You can download this application from the below link..