Introducción a Hibernate
De WikiEducator
< Usuario:Lmorillas | desarrollo web servidor | jee
Revisión a fecha de 23:53 30 ene 2013; Lmorillas (Discusión | contribuciones)
Objetivos
Tareas
Guión
|
BuildSessionFactory
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; // private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; Session session = null; try { try { Configuration cfg = new Configuration().addResource( "configuracion_file.hbm.xml").configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings( cfg.getProperties()).buildServiceRegistry(); sessionFactory = cfg.buildSessionFactory(serviceRegistry); } catch (Throwable ex) { System.err.println("Failed to create sessionFactory object."+ ex); throw new ExceptionInInitializerError(ex); } session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); // session.save(xxx); tx.commit(); } catch (Exception e) { System.out.println(e.getMessage()); } finally { session.close(); }