Diferencia entre revisiones de «Usuario:Lmorillas/desarrollo web servidor/jee/intro hibernate»
De WikiEducator
(→BuildSessionFactory) |
(→BuildSessionFactory) |
||
Línea 28: | Línea 28: | ||
== BuildSessionFactory == | == BuildSessionFactory == | ||
<source lang="java"> | <source lang="java"> | ||
+ | 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.ServiceRegistry; | ||
import org.hibernate.service.ServiceRegistryBuilder; | 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(); | ||
+ | } | ||
</source> | </source> |
Revisión de 23:53 30 ene 2013
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(); }