1 |
package dk.thoerup.pictureposterservice; |
2 |
|
3 |
|
4 |
import org.hibernate.SessionFactory; |
5 |
import org.hibernate.cfg.AnnotationConfiguration; |
6 |
|
7 |
public class HibernateUtil { |
8 |
private static final SessionFactory sessionFactory; |
9 |
|
10 |
static { |
11 |
try { |
12 |
|
13 |
// By using coded configuration and annotations we do not depend on external files |
14 |
AnnotationConfiguration cfg = new AnnotationConfiguration(); |
15 |
cfg.addAnnotatedClass(dk.thoerup.pictureposterservice.Posting.class); |
16 |
cfg.addAnnotatedClass(dk.thoerup.pictureposterservice.Comment.class); |
17 |
|
18 |
cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); |
19 |
cfg.setProperty("hibernate.connection.datasource", "jdbc/android"); |
20 |
|
21 |
cfg.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider"); |
22 |
|
23 |
cfg.setProperty("hibernate.current_session_context_class", "thread"); |
24 |
cfg.setProperty("hibernate.show_sql", "true"); //set to false in production |
25 |
//cfg.setProperty("hibernate.hbm2ddl.auto", "none"); |
26 |
cfg.setProperty("hibernate.hbm2ddl.auto", "validate"); |
27 |
|
28 |
sessionFactory = cfg.buildSessionFactory(); |
29 |
|
30 |
//This is how we could load the config from file |
31 |
//sessionFactory = new AnnotationConfiguration().configure( new File("/Hibernate.cfg.xml")).buildSessionFactory(); |
32 |
} catch (Throwable ex) { |
33 |
throw new ExceptionInInitializerError(ex); |
34 |
} |
35 |
} |
36 |
|
37 |
public static SessionFactory getSessionFactory() { |
38 |
return sessionFactory; |
39 |
} |
40 |
} |