model.HibernateUltils2.java Source code

Java tutorial

Introduction

Here is the source code for model.HibernateUltils2.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 model;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 *
 * @author cuongdx
 */
public class HibernateUltils2 {

    public static SessionFactory getSessionFactory() {
        //        try
        //        {
        // To i tng cu hnh.
        Configuration configuration = new Configuration();
        // Mc nh n s ?c cu hnh trong file hibernate.cfg.xml
        // Bn c th ch nh r file nu mun:
        // configuration.configure("hiberante.cfg.xml");
        configuration.configure();

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        // To i tng SessionFactory
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
        //        }
        //        catch (Throwable ex)
        //      {
        //         System.err.println("Initial SessionFactory creation failed." + ex);
        //         throw new ExceptionInInitializerError(ex);
        //      }
    }

    public static void shutdown() {
        // Close caches and connection pools
        // Gii phng cache v Connection Pools.
        getSessionFactory().close();
    }
}