org.semtix.db.hibernate.HibernateUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.semtix.db.hibernate.HibernateUtil.java

Source

/*
 * Semtix Semesterticketbroverwaltungssoftware entwickelt fr das
 *        Semesterticketbro der Humboldt-Universitt Berlin
 *
 * Copyright (c) 2015. Michael Mertins (MichaelMertins@gmail.com)
 * 2011-2014 Jrgen Schmelzle (j.schmelzle@web.de)
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as
 *     published by the Free Software Foundation, either version 3 of the
 *     License, or (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.semtix.db.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.semtix.config.SettingsExternal;

import java.io.File;

/**
 * Klasse fr Hibernate Sessions
 *
 * Created by MM on 02.02.15.
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {

        Configuration configuration;
        try {

            String path = SettingsExternal.HIBERNATE_CONF_XML;

            if (path.trim().length() <= 1) {
                configuration = new Configuration().configure();
            } else {
                // Create the SessionFactory from hibernate.cfg.xml
                configuration = new Configuration().configure(new File(path));
            }

        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            configuration = new Configuration().configure();
        }

        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());

        return configuration.buildSessionFactory(builder.build());
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

}