1 package org.objectweb.rentacar.persistance.util; 2 3 import java.util.Properties ; 4 5 import org.hibernate.HibernateException; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.cfg.AnnotationConfiguration; 9 import org.objectweb.rentacar.persistance.bo.Address; 10 import org.objectweb.rentacar.persistance.bo.Agency; 11 import org.objectweb.rentacar.persistance.bo.Car; 12 import org.objectweb.rentacar.persistance.bo.CentralOffice; 13 import org.objectweb.rentacar.persistance.bo.Contact; 14 import org.objectweb.rentacar.persistance.bo.Partner; 15 import org.objectweb.rentacar.persistance.bo.Reservation; 16 import org.objectweb.rentacar.util.RentacarConfigHelper; 17 18 23 public class HibernateUtil { 24 25 private static final SessionFactory sessionFactory; 26 private static Session session; 27 28 static { 29 try { 30 Properties props = new Properties (); 31 32 props.setProperty("show_sql" , RentacarConfigHelper.getProperty("hibernate-configuration", "show_sql")); 33 props.setProperty("hibernate.connection.driver_class" , RentacarConfigHelper.getProperty("hibernate-configuration", "hibernate.connection.driver_class")); 34 props.setProperty("hibernate.connection.url" , RentacarConfigHelper.getProperty("hibernate-configuration", "hibernate.connection.url")); 35 props.setProperty("hibernate.connection.username" , RentacarConfigHelper.getProperty("hibernate-configuration", "hibernate.connection.username")); 36 props.setProperty("hibernate.connection.password" , RentacarConfigHelper.getProperty("hibernate-configuration", "hibernate.connection.password")); 37 props.setProperty("hibernate.dialect" , RentacarConfigHelper.getProperty("hibernate-configuration", "hibernate.dialect")); 38 39 AnnotationConfiguration configuration = new AnnotationConfiguration(); 40 configuration.addProperties(props); 41 42 43 sessionFactory = configuration 44 .addPackage("org.objectweb.rentacar.persistance.bo") 45 .addAnnotatedClass(Address.class) 46 .addAnnotatedClass(Agency.class) 47 .addAnnotatedClass(Car.class) 48 .addAnnotatedClass(CentralOffice.class) 49 .addAnnotatedClass(Contact.class) 50 .addAnnotatedClass(Partner.class) 51 .addAnnotatedClass(Reservation.class) 52 .buildSessionFactory(); 53 } catch (Throwable ex) { 54 throw new ExceptionInInitializerError (ex); 56 } 57 } 58 59 public static Session getSession() 60 throws HibernateException { 61 if(session!=null){ 62 return session; 63 } 64 session=sessionFactory.openSession(); 65 return session; 66 } 67 68 } 69 | Popular Tags |