wersoft.defaultproject.model.dao.IndexHibernate.java Source code

Java tutorial

Introduction

Here is the source code for wersoft.defaultproject.model.dao.IndexHibernate.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 wersoft.defaultproject.model.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import wersoft.defaultproject.model.entities.Resource;

/**
 *
 * @author Will
 */
public class IndexHibernate {

    //mtodo thread-safe
    public static synchronized Session openSession() {

        Configuration conf = new Configuration();
        conf.configure();
        //ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry(); 
        StandardServiceRegistryBuilder serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(conf.getProperties());
        SessionFactory sessionFactory = conf.buildSessionFactory(serviceRegistry.build());
        Session session = sessionFactory.openSession();

        return session;
    }
}