1 |
package dk.thoerup.hibertest1;
|
2 |
|
3 |
import org.hibernate.SessionFactory;
|
4 |
import org.hibernate.cfg.AnnotationConfiguration;
|
5 |
|
6 |
|
7 |
public class HibernateUtil {
|
8 |
private static final SessionFactory sessionFactory;
|
9 |
static {
|
10 |
try {
|
11 |
// Create the SessionFactory from hibernate.cfg.xml
|
12 |
//sessionFactory = new AnnotationConfiguration().configure( new File("/Workspace/Hibertest1/Hibernate.cfg.xml")).buildSessionFactory();
|
13 |
|
14 |
//By using coded configuration and Annotations we do not depend on any external files
|
15 |
//this could be initialised in in a jee contextlistener, maybe reading parameters from web.xml
|
16 |
//See: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html
|
17 |
AnnotationConfiguration cfg = new AnnotationConfiguration();
|
18 |
cfg.addAnnotatedClass(dk.thoerup.hibertest1.Employee.class);
|
19 |
cfg.addAnnotatedClass(dk.thoerup.hibertest1.Department.class);
|
20 |
cfg.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
|
21 |
cfg.setProperty("hibernate.connection.url", "jdbc:postgresql://192.168.10.5/test");
|
22 |
cfg.setProperty("hibernate.connection.username", "torben");
|
23 |
cfg.setProperty("hibernate.connection.password", "nielsen");
|
24 |
cfg.setProperty("hibernate.connection.pool_size", "1");
|
25 |
cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
|
26 |
cfg.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
|
27 |
|
28 |
|
29 |
cfg.setProperty("hibernate.current_session_context_class", "thread");
|
30 |
cfg.setProperty("hibernate.show_sql", "true");
|
31 |
|
32 |
cfg.setProperty("hibernate.hbm2ddl.auto", "none");
|
33 |
|
34 |
sessionFactory = cfg.buildSessionFactory();
|
35 |
|
36 |
} catch (Throwable ex) {
|
37 |
// Make sure you log the exception, as it might be swallowed
|
38 |
System.err.println("Initial SessionFactory creation failed." + ex);
|
39 |
throw new ExceptionInInitializerError(ex);
|
40 |
}
|
41 |
}
|
42 |
|
43 |
public static SessionFactory getSessionFactory() {
|
44 |
return sessionFactory;
|
45 |
}
|
46 |
} |