47 lines
1.4 KiB
Java
47 lines
1.4 KiB
Java
package im.rosetta.database;
|
|
|
|
import org.hibernate.Session;
|
|
import org.hibernate.SessionFactory;
|
|
import org.hibernate.cfg.Configuration;
|
|
|
|
public class HibernateUtil {
|
|
private static final SessionFactory sessionFactory;
|
|
|
|
static {
|
|
try {
|
|
Configuration cfg = new Configuration().configure();
|
|
|
|
String host = System.getenv("DB_HOST");
|
|
String port = System.getenv("DB_PORT");
|
|
String user = System.getenv("DB_USER");
|
|
String pass = System.getenv("DB_PASSWORD");
|
|
String name = System.getenv("DB_NAME");
|
|
String url = String.format("jdbc:postgresql://%s:%s/%s", host, port, name);
|
|
cfg.setProperty("hibernate.connection.url", url);
|
|
cfg.setProperty("hibernate.connection.username", user);
|
|
cfg.setProperty("hibernate.connection.password", pass);
|
|
|
|
sessionFactory = cfg.buildSessionFactory();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new ExceptionInInitializerError("Error initializing Hibernate: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
public static SessionFactory getSessionFactory() {
|
|
return sessionFactory;
|
|
}
|
|
|
|
public static Session getCurrentSession() {
|
|
return sessionFactory.getCurrentSession();
|
|
}
|
|
|
|
public static Session openSession() {
|
|
return sessionFactory.openSession();
|
|
}
|
|
|
|
public static void shutdown() {
|
|
sessionFactory.close();
|
|
}
|
|
}
|