hibernate.demo.HibernateSingleton.java Source code

Java tutorial

Introduction

Here is the source code for hibernate.demo.HibernateSingleton.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 hibernate.demo;

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

/**
 *
 * @author kmhasan
 */
public class HibernateSingleton {
    private static HibernateSingleton instance = new HibernateSingleton();
    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    private HibernateSingleton() {
        Configuration configuration = new Configuration();
        configuration.configure(HibernateDemo.class.getResource("hibernate.cfg.xml"));
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    }

}