package dk.thoerup.hibertest1; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from hibernate.cfg.xml //sessionFactory = new AnnotationConfiguration().configure( new File("/Workspace/Hibertest1/Hibernate.cfg.xml")).buildSessionFactory(); //By using coded configuration and Annotations we do not depend on any external files //this could be initialised in in a jee contextlistener, maybe reading parameters from web.xml //See: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html AnnotationConfiguration cfg = new AnnotationConfiguration(); cfg.addAnnotatedClass(dk.thoerup.hibertest1.Employee.class); cfg.addAnnotatedClass(dk.thoerup.hibertest1.Department.class); cfg.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver"); cfg.setProperty("hibernate.connection.url", "jdbc:postgresql://192.168.10.5/test"); cfg.setProperty("hibernate.connection.username", "torben"); cfg.setProperty("hibernate.connection.password", "nielsen"); cfg.setProperty("hibernate.connection.pool_size", "1"); cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); cfg.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider"); cfg.setProperty("hibernate.current_session_context_class", "thread"); cfg.setProperty("hibernate.show_sql", "true"); cfg.setProperty("hibernate.hbm2ddl.auto", "none"); sessionFactory = cfg.buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }