br.com.loja.framework.hibernate.HibernateUtil.java Source code

Java tutorial

Introduction

Here is the source code for br.com.loja.framework.hibernate.HibernateUtil.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.loja.framework.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author David Luiz
 */
public class HibernateUtil {

    private static final SessionFactory SESSION_FACTORY = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml");
            return configuration.buildSessionFactory();

        } catch (Exception ex) {
            // Make sure you log the exception, as it might be swallowed
            throw new RuntimeException("Falhar ao iniciar configuracao de acesso ao banco de dados", ex);
        }
    }

    public static Session getSession() {
        return SESSION_FACTORY.getCurrentSession();

    }

}