package dk.thoerup.pictureposterservice; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { // By using coded configuration and annotations we do not depend on external files AnnotationConfiguration cfg = new AnnotationConfiguration(); cfg.addAnnotatedClass(dk.thoerup.pictureposterservice.Posting.class); cfg.addAnnotatedClass(dk.thoerup.pictureposterservice.Comment.class); cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); cfg.setProperty("hibernate.connection.datasource", "jdbc/android"); cfg.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider"); cfg.setProperty("hibernate.current_session_context_class", "thread"); cfg.setProperty("hibernate.show_sql", "true"); //set to false in production //cfg.setProperty("hibernate.hbm2ddl.auto", "none"); cfg.setProperty("hibernate.hbm2ddl.auto", "validate"); sessionFactory = cfg.buildSessionFactory(); //This is how we could load the config from file //sessionFactory = new AnnotationConfiguration().configure( new File("/Hibernate.cfg.xml")).buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }