Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. package org.sistema.hibernate.tareaHibernate;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.cfg.Configuration;
  6. import org.hibernate.service.ServiceRegistry;
  7. import org.hibernate.service.ServiceRegistryBuilder;
  8.  
  9. /**
  10. * Represents a entity which handles the session with the database.
  11. *
  12. * @author Eugenia Pérez Martínez
  13. */
  14. public class HibernateSession {
  15. private static final SessionFactory sessionFactory = buildSessionFactory();
  16. private static Session session;
  17.  
  18. /**
  19. * Based on hibernate.cfg.xml configuration creates a SessionFactory
  20. *
  21. * @return the session factory
  22. */
  23. private static SessionFactory buildSessionFactory() {
  24. Configuration configuration = new Configuration();
  25. configuration.configure();
  26. ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
  27. .applySettings(configuration.getProperties())
  28. .buildServiceRegistry();
  29. SessionFactory sessionFactory = configuration
  30. .buildSessionFactory(serviceRegistry);
  31. return sessionFactory;
  32. }
  33.  
  34. /**
  35. * this gives the desired session factory
  36. *
  37. * @return hibernate Session Factory instance
  38. */
  39. public static SessionFactory getSessionFactory() {
  40. return sessionFactory;
  41. }
  42.  
  43. /**
  44. * gives the current Session
  45. *
  46. * @return Hibernate Session
  47. */
  48. public static Session getSession() {
  49. if (null == session) {
  50. session = sessionFactory.openSession();
  51. }
  52. return session;
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement