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 SessionManager; import java.util.Properties; import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.Session; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; /** * * @author jomendez */ public abstract class SessionManager { // Instancia un objeto del tipo Configuration static Configuration config = new Configuration(); static SessionFactory factory; /** * Creates a new instance of EducacionITSessionManager */ public SessionManager() { } public static Session getSession() throws HibernateException { // Registra los mappers en la configuracion registerMappers(config); // Establece las propiedades de configuracion config.setProperties(getHibernateProperties()); // Guarda la fabrica de sesiones StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(config.getProperties()); factory = config.buildSessionFactory(builder.build()); // Retorna una sesion de trabajo return factory.openSession(); } public static void ShutDown() { try { factory.close(); StandardServiceRegistryBuilder.destroy(factory.getSessionFactoryOptions().getServiceRegistry()); //Seems like a bug, we need to explicitly destroy service registry!! } catch (Throwable t) { System.err.println("Exception while closing session factory: " + t); } } private static Properties getHibernateProperties() { // Instancia un objeto del tipo Properties Properties props = new Properties(); // Establece el driver de conexion dependiente del RDBMS props.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); // Establece la url de conexion dependiente del RDBMS props.put("hibernate.connection.url", "jdbc:mysql://190.228.29.58:3306/clubm_bd"); // Establece el usuario props.put("hibernate.connection.username", "user_clubmasco"); // Establece la clave props.put("hibernate.connection.password", "Clubmascotero1"); // Establece el dialecto a utilizar props.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); // Establece el uso de logging, deber existir el archivo log4j.properties props.put("hibernate.show_sql", "true"); // Retorna las propiedades return props; } private static void registerMappers(Configuration config) throws MappingException { config.addAnnotatedClass(objetos.Usuario.class); } }