Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. package com.psc.db;
  2.  
  3. import com.psc.db.mapped.Part;
  4. import org.hibernate.HibernateException;
  5. import org.hibernate.Session;
  6. import org.hibernate.SessionFactory;
  7. import org.hibernate.cfg.Configuration;
  8.  
  9.  
  10. public class HibernateSessionHandler {
  11.     private static final SessionFactory sessionFactory;
  12.  
  13.     static {
  14.         try {
  15.             sessionFactory = new Configuration().addAnnotatedClass(Part.class).configure("hibernate.cfg.xml").buildSessionFactory();
  16.         } catch (Throwable e) {
  17.             System.err.println("Error in creating SessionFactory object." + e.getMessage());
  18.             throw new ExceptionInInitializerError(e);
  19.         }
  20.     }
  21.  
  22.     public static SessionFactory getSessionFactory() {
  23.         return sessionFactory;
  24.     }
  25.  
  26.     public static Session getCurrentSession() {
  27.         try {
  28.             return sessionFactory.getCurrentSession();
  29.         } catch (HibernateException he) {
  30.             return sessionFactory.openSession();
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement