Java tutorial
/* * 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 com.netcore.hsmart.hibernate; import com.netcore.hsmart.AppConstants; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; /** * * @author root */ public class HibernateUtil { private static final SessionFactory SESSION_FACTORY = buildSessionFactory(); // Hibernate 5: private static SessionFactory buildSessionFactory() { try { Configuration configuration = new Configuration(); //configuration.configure("hibernate.cfg.xml"); configuration.setProperty("hibernate.connection.driver_class", AppConstants.getWriteDbDriverClass()); configuration.setProperty("hibernate.connection.username", AppConstants.getWriteDbUsername()); configuration.setProperty("hibernate.connection.password", AppConstants.getWriteDbPassword()); configuration.setProperty("hibernate.connection.url", "jdbc:" + AppConstants.getWriteDbJdbcType() + "://" + AppConstants.getWriteDbHost() + ":" + AppConstants.getWriteDbPort() + "/" + AppConstants.getWriteDbName()); configuration.setProperty("hibernate.dialect", AppConstants.getWriteDbDialect()); configuration.setProperty("hibernate.show_sql", AppConstants.getWriteDbShowSql()); configuration.setProperty("hibernate.current_session_context_class", AppConstants.getWriteDbCtxClass()); configuration.setProperty("hibernate.c3p0.min_size", AppConstants.getWriteDbPoolMinSize()); configuration.setProperty("hibernate.c3p0.max_size", AppConstants.getWriteDbPoolMaxSize()); configuration.setProperty("hibernate.c3p0.timeout", AppConstants.getWriteDbTimeout()); configuration.setProperty("hibernate.connection.autocommit", AppConstants.getWriteDbAutoCommit()); configuration.setProperty("hibernate.connection.release_mode", AppConstants.getWriteDbReleaseMode()); configuration.setProperty("hibernate.c3p0.idle_test_periods", AppConstants.getWriteDbIdleTestsPeriod()); configuration.addAnnotatedClass(com.netcore.hsmart.hibernate.entities.SmsRequestData.class); configuration.addAnnotatedClass(com.netcore.hsmart.hibernate.entities.DlrResponseData.class); configuration.addAnnotatedClass(com.netcore.hsmart.hibernate.entities.DlrStatusMap.class); return configuration.buildSessionFactory( new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build()); // Create the ServiceRegistry from hibernate.cfg.xml // ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()// // .configure("hibernate.cfg.xml").build(); // // // Create a metadata sources using the specified service registry. // Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build(); // // return metadata.getSessionFactoryBuilder().build(); } catch (HibernateException ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return SESSION_FACTORY; } public static void shutdown() { // Close caches and connection pools getSessionFactory().close(); } }